Чтение только целых чисел из текстового файла и добавление значения для каждого найденного
Я пытаюсь прочитать текстовый файл, который содержит строки с целыми числами. Я хотел бы иметь возможность получить только целые числа из этого файла и добавить значение каждого для создания итога. Мне удалось прочитать файл нормально и смог идентифицировать целые и не. Как бы я мог сложить целые числа вместе?
public void goalCount() throws FileNotFoundException {
int goals=0;
double total=0;
try {
Scanner scan = new Scanner(new File("validtest.txt.txt"));
// find the next int token and print it
// loop for the whole scanner
while (scan.hasNext()) {
// if the next is a int, print found and the int
if (scan.hasNextInt()) {
System.out.println("Found :" + scan.nextInt());
//goals = scan.nextInt();
//total+= goals;
}
// if no int is found, print "Not Found:" and the token
System.out.println("Not Found :" + scan.next());
}
// close the scanner
scan.close();
} catch(Exception e) {
}
System.out.println("Total Goals:"+total); //error handling
}
Пример текстового файлаLeeds United : Liverpool : 1 : 2
Chelsea : Manchester City : 1 : 1
Aston Villa : Middlesbrough : 3 : 1
Tottenham Hotspur : Stoke City : 0 : 0