Java Scanner错误

时间:2014-09-01 08:32:48

标签: java

运行以下代码时出现Scanner错误: 怎么会这样?

import java.util.Scanner;

public class ComputeLoan {
    public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    //Enter annual interest rate in percentage e.g., 7.25%
    System.out.print("Enter annual interest rate in, e.g., 7.25%: ");
    double annualInterestRate = input.nextDouble();

    // Obtain monthly interest rate
    double monthlyInterestRate = annualInterestRate / 1200;

    //Enter number of years
    System.out.print(
      "Enter number of years as an integer, e.g., 5: ");
    int numberOfYears = input.nextInt();

    // Enter loan amount
    double loanAmount = input.nextDouble();

    // Calculate payment
    double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 +     
    monthlyInterestRate, numberOfYears * 12));

    double totalPayment = monthlyPayment * numberOfYears * 12;


    // Display results
    System.out.println("The monthly payment is $" + (int) (monthlyPayment * 100) / 100.0);
    System.out.println("The total payment is $" + (int) (totalPayment * 100) / 100.0);

    }
}

控制台:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at ComputeLoan.main(ComputeLoan.java:10)

3 个答案:

答案 0 :(得分:4)

这里:

 System.out.print("Enter annual interest rate in, e.g., 7.25%: ");
 double annualInterestRate = input.nextDouble();

当您在控制台上输入值时,请将其输入为7.25,将 NOT 输入为7.25%

遵循的良好做法: 在命令行上接受用户的值时,请始终以String的形式接受它们,即input.nextLine(),然后使用解析方法将其解析为适当的类型。如果解析时发生异常,则表示用户未正确输入输入,通知用户并继续输入。

示例:

while(true) {
      try {
            System.out.print("Enter annual interest rate in, e.g., 7.25%: ");
            double annualInterestRate = Double.parseDouble(input.nextLine());
            break;
          } 
     catch(NumberFormatException nfe) {
                System.out.print("Wrong input,enter again");
            }
       }

答案 1 :(得分:3)

原因:

InputMismatchException

  

由扫描程序抛出,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

发生异常是因为您正在输入String" 7.25%"并尝试使用input.nextInt()和input.nextDouble()从Scanner中检索Double和int。

解决方案:

您可以从扫描仪中读取行,然后使用

解析字符串中的值

从用户读取字符串并解析双值:

    // Enter annual interest rate in percentage e.g., 7.25%
    System.out.print("Enter annual interest rate in, e.g., 7.25%: ");
     //read the line
    String inputStr = input.nextLine();

    //parse the double value after removing the % sign
    double annualInterestRate = Double.parseDouble(inputStr.replace("%", "")); 

类似地:

    // Enter number of years
    System.out.print("Enter number of years as an integer, e.g., 5: ");
     //read the line
    inputStr = input.nextLine();
    //parse the int value from the input String
    int numberOfYears = Integer.parseInt(inputStr);

答案 2 :(得分:2)

当我输入以下内容时,我会得到输出:

输入年利率,例如7.25%:7.25

以整数形式输入年数,例如5:7

第3

每月付款为0.04美元

总付款为$ 3.83

流程已完成退出代码0

我希望你知道退出代码0是什么:)

但是,您的代码存在问题,

  1. 我必须输入一些随机整数,在我的情况下,“3”表示每月付款和总付款流程。

  2. 就像已经指出的那样,你没有输入7.25%。 input.nextDouble()占位符不期望保持那种数据,简单地说。输入任何“”号码即可。

  3. 谢谢