Why get i InputMismatchException when i use scanner sc.nextDouble()

后端 未结 1 606
Happy的楠姐
Happy的楠姐 2021-01-24 02:08

I\'d like to read data from a txt file, but I get InputMismatchException when I call nextDouble() method. Even though I am using the

相关标签:
1条回答
  • 2021-01-24 02:21

    Blame the French locale: it uses comma as a decimal separator, so 1.9 fails to parse.

    Replacing 1.9 with 1,9 fixes the problem (demo 1). If you would like to parse 1.9, use Locale.US instead of Locale.FRENCH (demo 2).

    A second problem in your code is the use of \\n as the separator. You should use a single backslash, otherwise words that contain n would break your parsing logic.

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