扫描仪双精度值 - InputMismatchException [英] Scanner double value - InputMismatchException

查看:27
本文介绍了扫描仪双精度值 - InputMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以最简单的方式使用扫描仪:

代码:

双气、效率、距离、成本;扫描仪scanner = new Scanner(System.in);System.out.print("请输入油箱中的汽油加仑数:");气体=scanner.nextDouble();System.out.print("请输入燃油效率:");效率=scanner.nextDouble();

但是在第一次输入 5.1 之后它抛出:

线程main"中的异常 java.util.InputMismatchException在 java.util.Scanner.throwFor(Scanner.java:909)在 java.util.Scanner.next(Scanner.java:1530)在 java.util.Scanner.nextDouble(Scanner.java:2456)在 udacity.MileagePrinter.main(MileagePrinter.java:59)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:601)在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

JavaDocs 状态:<块引用>

被扫描器抛出,表明检索到的令牌与
预期类型的​​模式,或者令牌超出预期类型的​​范围.

但在我看来一切正常,应该可以正常工作.

问题:

  • 为什么会在这种情况下发生?
  • 如何避免这种麻烦?

解决方案

您应该为您的扫描仪精确定位.

Scanner 扫描仪 = new Scanner(System.in).useLocale(Locale.US);

来自 doc :

<块引用>

此类的一个实例能够扫描标准格式以及扫描仪区域设置的格式.一种扫描器的初始语言环境是由Locale.getDefault() 方法;它可以通过useLocale(java.util.Locale) 方法

本地化格式根据以下内容定义特定语言环境的参数取自该语言环境的DecimalFormat 对象,df,及其和 DecimalFormatSymbols 对象,dfs.

因此,您的默认语言环境肯定使用 DecimalFormat,它期望逗号作为十进制分隔符而不是点.

I tried use scanner at easiest way:

Code:

double gas, efficiency, distance, cost;
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of gallons of gas in the tank: ");
gas = scanner.nextDouble();
System.out.print("Enter the fuel efficiency: ");
efficiency = scanner.nextDouble();

But after first input 5.1 it throws:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at udacity.MileagePrinter.main(MileagePrinter.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

The JavaDocs state:

Thrown by a Scanner to indicate that the token retrieved does not match the
pattern for the expected type, or that the token is out of range for the expected type.

But to my mind all look correctly, and should work OK.

Questions:

  • Why this happen at this situation?
  • How to circumvent this trouble?

解决方案

You should precise a Locale for your Scanner.

Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

From the doc :

An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the Locale.getDefault() method; it may be changed via the useLocale(java.util.Locale) method

The localized formats are defined in terms of the following parameters, which for a particular locale are taken from that locale's DecimalFormat object, df, and its and DecimalFormatSymbols object, dfs.

So your default locale use certainly a DecimalFormat that expect a comma as a decimal delimiter instead of a dot.

这篇关于扫描仪双精度值 - InputMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆