I have such a file:
3
0.1
0.4
1
InputStream s = Main.class.getResourceAsStream("/file.txt");
Scanner scanner = new Scanner(s);
//Scanner scanner = new Scanner(path); **EDITED**
System.out.println(scanner.nextInt());
System.out.println(scanner.nextDouble());
System.out.println(scanner.nextDouble());
System.out.println(scanner.nextInt());
3 0.1
0.4
1
As per your description it looks like a locale problem. As stated by Scanner
docs:
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 theuseLocale(java.util.Locale)
method. Thereset()
method will reset the value of the scanner's locale to the initial locale regardless of whether it was previously changed.
I presume your default locale is Polish locale, which decimal separator is a comma. You might want to change your input files to use your locale decimal separator or use a locale which decimal separator is a dot. For example, you could add the following line after the Scanner
initialization:
scanner.useLocale(Locale.US);