From 900ee5e2d1f57ab83a13b42a3e938bbaf013cdc8 Mon Sep 17 00:00:00 2001 From: Fennel Kora Date: Fri, 2 Dec 2022 12:03:24 -0500 Subject: [PATCH] Fixed bug that caused error when EOF reached Changes to be committed: modified: 2022/day01/CalorieCount.java --- 2022/day01/CalorieCount.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/2022/day01/CalorieCount.java b/2022/day01/CalorieCount.java index 0b89e56..d6f5577 100644 --- a/2022/day01/CalorieCount.java +++ b/2022/day01/CalorieCount.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.util.Scanner; public class CalorieCount { - public static void main(String[] args) { + public static void main (String[] args) { try (Scanner inputReader = new Scanner(Paths.get("input.txt"))) { int maxCals = 0; while (inputReader.hasNext()) { @@ -13,12 +13,16 @@ public class CalorieCount { inLine = inputReader.nextLine(); if (!inLine.isEmpty()) { currentCals += Integer.parseInt(inLine); + System.out.printf("Current elf total is %d%n", currentCals); } - } while (!inLine.isEmpty()); + } while (!inLine.isEmpty() && inputReader.hasNext()); - if (currentCals > maxCals) {maxCals = currentCals;} + if (currentCals > maxCals) { + maxCals = currentCals; + System.out.printf("New Max Cals: %d%n", maxCals); + } } - System.out.printf("The large calories carried is: %d", maxCals); + System.out.printf("The large calories carried is: %d%n", maxCals); } catch (IOException e) { System.out.println("IOException"); e.printStackTrace();