英文:
mortgage calculator (java.util.InputMismatchException)
问题
抱歉,我无法提供代码部分的翻译,但我可以帮你解释问题和提供解决方案。
问题在于,当你输入年利率(如3.92)时,它给出了"java.util.InputMismatchException"错误。这是因为你的程序预期输入的是浮点数,但你的输入使用了双引号,使其成为字符串。为了解决这个问题,你可以按照以下方式修改代码:
将这两行代码:
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
改为:
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
这将消除错误,让你能够正确输入年利率。
希望这对你有帮助!如果有其他问题,请随时提问。
英文:
hello i have a problem when following a tutorial in youtube (the code is in java, i am using intelliji idea)
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] arg) {
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(Locale.US).format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
}
The problem is when i input the Annual Interest Rate (like 3.92) it give me the following error:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:947)
at java.base/java.util.Scanner.next(Scanner.java:1602)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2505)
at com.codewithtamer.Main.main(Main.java:17)
And thank you for helping a beginner like me :), + i think i somehow messed up the format of this post sorry
答案1
得分: 2
如果您想在Scanner中使用与默认区域设置(Java从操作系统获取的区域设置)不同的区域设置,可以这样做:
class App {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println(s.locale());
s.useLocale(Locale.US);
System.out.println(s.nextFloat());
}
}
这将显示您当前的区域设置(对我来说,它会打印en_AU
),然后将扫描仪设置为使用不同的区域设置。
Locale.US
将需要 .
作为小数分隔符,Locale.FRANCE
需要 ,
,依此类推。
英文:
If you want to use a Locale with your Scanner which is different to your default locale (the one Java gets from the operating system) you can do this:
class App {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println(s.locale());
s.useLocale(Locale.US);
System.out.println(s.nextFloat());
}
}
That will show your current locale (for me it prints en_AU
) and then sets the scanner to use a different locale.
Locale.US
with require .
as a decimal separator, Locale.FRANCE
requires ,
and so on.
答案2
得分: 0
在某些版本的一些集成开发环境(IDE)中,java.util.Scanner以欧洲模式(即使用小数逗号)读取小数。根据这个StackOverflow的问答建议(https://stackoverflow.com/questions/28997094/scanner-next-throws-java-util-inputmismatchexception-for-float-but-not-for-in),观察到了这种情况。您可以考虑更改您的IDE版本,或者您可以使用逗号而不是小数点来表示百分比(不太理想),或者您可以使用Float.parseFloat()
,如下所示:
System.out.print("年度利率:");
float annualInterest = Float.parseFloat(scanner.next());
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
这应该可以使用小数点,因为Float.parseFloat()
不受位置影响,始终期望小数点。
英文:
Unfortunately in some versions of some IDEs the java.util.Scanner reads decimals in European mode (i.e. with a decimal comma). This has been observed in Eclipse and Netbeans as suggested by this StackOverflow Q&A (https://stackoverflow.com/questions/28997094/scanner-next-throws-java-util-inputmismatchexception-for-float-but-not-for-in). You could look into changing the version of your IDE, or you could write the percent with a comma (not ideal), or you can instead rely on Float.parseFloat()
such as in the following:
System.out.print("Annual Interest Rate: ");
float annualInterest = Float.parseFloat(scanner.next());
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
This should work with a decimal point as Float.parseFloat()
is not location-dependent and always expects a decimal point.
答案3
得分: -1
根据Scanner#nextFloat的JavaDoc,
> "... 如果下一个标记不匹配Float正则表达式,或超出范围,则抛出InputMismatchException异常...".
对于nextInt方法也是一样的——如果值不是整数值,则会抛出异常。
尽管如此,需要注意的是,您可以使用nextFloat来捕获整数值。
例如,
Principal: 1
Annual Interest Rate: 2
Period (Years): 3
Mortgage: $0.03
以及,
Principal: 1.23
在主线程中的异常"main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:947)
at java.base/java.util.Scanner.next(Scanner.java:1602)
at java.base/java.util.Scanner.nextInt(Scanner.java:2267)
at java.base/java.util.Scanner.nextInt(Scanner.java:2221)
at Example.main(Example.java:14)
英文:
As per the JavaDoc for Scanner#nextFloat,
> "... Throws [an] InputMismatchException if the next token does not match the Float regular expression, or is out of range ...".
And, the same goes for the nextInt method—the exception is thrown if the value is not an integer value.
Although, as a note, you can use the nextFloat to capture integer values.
For example,
Principal: 1
Annual Interest Rate: 2
Period (Years): 3
Mortgage: $0.03
And,
Principal: 1.23
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:947)
at java.base/java.util.Scanner.next(Scanner.java:1602)
at java.base/java.util.Scanner.nextInt(Scanner.java:2267)
at java.base/java.util.Scanner.nextInt(Scanner.java:2221)
at Example.main(Example.java:14)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论