繁体   English   中英

线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“ 0.06”

[英]Exception in thread “main” java.lang.NumberFormatException: For input string: “0.06”

我一直在尝试为学校项目制作一个简单的日食计划,但是在输入利率后,我一直得到这个计划。 一般而言,我对编码和编程还比较陌生,而从本月开始,java对我来说还是陌生的,因此不胜感激。 代码是这样的:

import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;


public class Interest {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);

    //Input ============================

    System.out.println("Initial loan total:");     //cost
    String cost;
    cost = in.nextLine();

    System.out.println("Down payment:");     //down
    String down;
    down = in.nextLine();

    System.out.println("Length of term:");     //term
    String term;
    term = in.nextLine();

    System.out.println("Interest rate (decimal form):");     //rate
    String rate;
    rate = in.nextLine();

    int principle1 = Integer.parseInt(cost) - Integer.parseInt(down);

    String hundred;
    hundred = "100";

    int interest = Integer.parseInt(rate) * Integer.parseInt(hundred);

    //Output ===========================

    Calendar c = Calendar.getInstance();
    System.out.format("%tB %td, %tY", c,c,c);
    System.out.println("");
    System.out.println("The initial cost of the loan is $" + cost);
    System.out.println("The down payment is $" + down);
    System.out.println("The principle is $" + principle1);
    System.out.println("The term is " + term + " months");
    System.out.println("The interest rate is " + interest + "%");
    System.out.println("The monthly patments are $");

    in.close();
}

}

当我运行该程序时,它让我投入了初始贷款,预付定金和期限,但是一旦我输入0.06作为利率,它就会给我错误消息。 我还想指出的是,我对代码中的数学工作原理了解有限。

问题是您正在尝试将0.06解析为Integer,而0.06是float 使用Float.parseFloat(rate); 您的兴趣应该是float interest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM