为什么在这段代码中我会得到InputMismatchException异常?

huangapple go评论108阅读模式
英文:

Why do I get InputMismatchException in this code?

问题

I'm getting:
'Exception in thread "main" java.util.InputMismatchException' in:
float annualInterest = scanner.nextFloat();
我遇到了以下问题:
'Exception in thread "main" java.util.InputMismatchException' 在这行代码中:
float annualInterest = scanner.nextFloat();
I have tried double and now float, getting same error.
我尝试了使用double和float,但仍然遇到相同的错误。
I put in a number with decimal (3.2) and getting this exception.
我输入了一个带小数点的数字(3.2),然后出现了这个异常。
Someone can tell me whats wrong in this code?
有人可以告诉我这段代码有什么问题吗?
Using JDK 14.
使用JDK 14。

英文:

I'm getting:
'Exception in thread "main" java.util.InputMismatchException' in:
float annualInterest = scanner.nextFloat();
I have tried double and now float, getting same error.
I put in a number with decimal (3.2) and getting this exception.
Someone can tell me whats wrong in this code?
Using JDK 14.

public class Main {

public static void main(String[] args) {
    final byte MONTHS_IN_YEAR = 12;
    final byte PERCENT = 100;

    Scanner scanner = new Scanner(System.in);

    System.out.print("Principal: ");
    int principal = scanner.nextInt();

    System.out.print("Annual Interest Rate: ");
    float annualInterest = scanner.nextFloat();
    float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;

    System.out.print("Period (Years): ");
    byte years = scanner.nextByte();
    int numberOfPayments = years * MONTHS_IN_YEAR;

    double mortgage = principal
                * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
                / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

    String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
    System.out.println("Mortgage: " + mortgageFormatted);
    }
}

答案1

得分: 0

> 在我的Mac上,当涉及到“年度利率:”并输入3.2时,它会崩溃,但如果我输入3,2,那么它就可以正常工作。

这是由于您系统设置的问题,确定了用于编写小数的标点符号。在Java中,我们将其称为“区域设置”。一个解决方案是更改系统设置,但这将更改计算机上每个程序的设置。目前,我建议您只是使用您的系统默认接受的格式输入数字。

如果您想在自己的Java程序中使用不同的设置,那么您需要设置与您希望使用的规则相对应的区域设置。不要与之对抗。将来,当您更多了解Java时,您可以开始学习有关区域设置以及如何在您的Java代码中设置它们的内容。

英文:

> On my Mac, when it comes to "Annual Interest Rate: " and I write in 3.2, it crashes, but if i write in 3,2 then it works.

This is due to your system settings that determine the punctuation used for writing decimal numbers. We call this a "locale" in Java. One solution is to change the system settings, but this will change the settings for every program on your computer. For now, I suggest you just enter numbers using the format that your system accepts by default.

If you want to use different settings just in your own Java program, then you need to set the locale that corresponds to the rules you want to use. Don't fight it. In the future when you know more about Java, you can start learning about locale and how to set them in your Java code.

答案2

得分: 0

根据该方法的文档所述

> 如果下一个令牌不匹配<i>Float</i>的正则表达式,或者超出范围,则抛出InputMismatchException异常

我猜测您的输入 (3.2) 不符合浮点数的当前区域设置格式。使用我的默认区域设置(Locale.GERMAN), 您需要输入“3,2”。

如果您想更改Scanner内部使用的Locale,可以使用Scanner.useLocale来实现。

英文:

As the documentation of the Method says

> @throws InputMismatchException if the next token does not match the <i>Float</i> regular expression, or is out of range

I guess your Input (3.2) does not match the format of a float for your current locale. With my default locale (Locale.GERMAN) I would need to type "3,2".

If you want to change the Locale your Scanner uses internally you can do so by using Scanner.useLocale.

huangapple
  • 本文由 发表于 2020年7月28日 02:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定