leonz leonz - 9 months ago 59
Java Question

Scanner is reading numbers wrong from Standard IO. Java

I have this code:

for (int i = 0; i < N; i ++) {
unos[i].x = input.nextDouble();
unos[i].R = input.nextFloat();

If I input 2.177, unos[i].x will be 2177.0, which it clearly isn't. If I input 3.15, it will give me Input Mismatch Exception. What am I doing wrong?


If I input 2.177, unos[i].x will be 2177.0

well this is because you are reading doubles with a locale where decimal part is with coma instead of dot.

YOU have to tell the scanner how to read the decimal part...

I mean, set the locale


Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
unos[i].x = input.nextDouble();