Scanner double value - InputMismatchException

前端 未结 2 1108
情深已故
情深已故 2020-11-22 15:53

I tried use scanner at easiest way:

Code:

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

System.         


        
相关标签:
2条回答
  • 2020-11-22 16:03

    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.

    0 讨论(0)
  • 2020-11-22 16:21

    Make sure that you are using the correct locale

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

    Maybe you are using a locale where "," is the decimal delimiter

    0 讨论(0)
提交回复
热议问题