diff --git "a/.\\MainSevenBefore.java" "b/.\\MainSeven.java" index 5b72238..f8be922 100644 --- "a/.\\MainSevenBefore.java" +++ "b/.\\MainSeven.java" @@ -1,16 +1,43 @@ import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -public class MainSevenBefore { - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new FileReader("scores.txt")); - int sum = 0; - String line; - while ((line = br.readLine()) != null) { - sum += Integer.parseInt(line); +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()); } - br.close(); - System.out.println(sum); } }