I want to use the scanner to enter double values. At input 0.5 exception is thrown from the nextDouble() method, and at 0,5 , everything is ok.
Is this normal default behavior? How to change to input with a dot?
I want to use the scanner to enter double values. At input 0.5 exception is thrown from the nextDouble() method, and at 0,5 , everything is ok.
Is this normal default behavior? How to change to input with a dot?
The scanner uses the locale to determine the input values. To define a point instead of a comma as a decimal separator, you need to change the locale for example
Scanner scanner = new Scanner(System.in).useLocale(Locale.US); In the comments to the en-SO response, another entry is given.
Most likely the matter is in the locale. For some, a comma is used. If you need a response to a point, then you can write this:
Scanner inp = new Scanner(System.in); inp.useLocale(Locale.ENGLISH); Should work
ps Locales are in java.util.Locale
Source: https://ru.stackoverflow.com/questions/586976/
All Articles