You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class MainSeven {
private static int parseLine(String line) throws InvalidInputException {
String trimmed = line.trim();
if (trimmed.isEmpty()) {
throw new InvalidInputException();
}
try {
return Integer.parseInt(trimmed);
} catch (NumberFormatException e) {
throw new InvalidInputException(trimmed, e);
}
}
public static void main(String[] args) {
try (FileReader fr = new FileReader("scores.txt")) {
BufferedReader br = new BufferedReader(fr);
int sum = 0;
String line;
while ((line = br.readLine()) != null) {
try {
sum += parseLine(line);
} catch (InvalidInputException e) {
System.out.println(e.getMessage());
}
}
br.close();
System.out.println(sum);
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}