I've got a .txt file and here's its content:
1.1, 1.2, 1.3, 2.0, 1.8
1.3, aa, 4.5, 6.7, 2.1
3.5, 7.7, 9.9, 4.1, 2.1
I've got to load all lines from it, and if there'a double, sum it and show at the end.
I've written this program:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
File main3File = new File("Main3.txt");
double sum = 0;
try {
Scanner scan = new Scanner(main3File);
scan.useDelimiter(", ");
while (scan.hasNextLine()) {
if (scan.hasNextDouble()) {
sum += scan.nextDouble();
System.out.println(sum);
}
}
} catch (FileNotFoundException ex) {
System.out.println("Blad");
}
System.out.println("Suma: " + sum);
}
}
Here's the output:
1.1
2.3
3.5999999999996
5.6
So it sum the first three numbers, then stop and the while loop doesn't stop (if I write something under 'if', it shows without stopping). It doesn't sum the last number (1.8) and it doesn't go to the next line.
I guess it's something wrong with delimeter, right? But I don't have idea how to change it. Do you have an idea?