leonz leonz - 1 month ago 15
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();
System.out.println(unos[i].x);
unos[i].R = input.nextFloat();
System.out.println(unos[i].R);
}


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?

Answer

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

Example:

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