Loading AcceptFile.java +7 −7 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ import java.io.IOException; public class AcceptFile { public static void main(String[] args) { String fileName = "wrongInput.txt"; // Replace with the name of text file to check String fileName = "correctInput.txt"; // Replace with the name of text file to check // BufferedReader is used to parse through text file line by line try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { Loading @@ -23,6 +23,9 @@ public class AcceptFile { line = br.readLine(); int height = Integer.parseInt(line.trim()); //to save the point in the file after reading width and height (line 3) br.mark(1000); if (width <= 0 || height <= 0) { System.out.println("Error: width and height must be positive integers."); return; Loading @@ -33,7 +36,7 @@ public class AcceptFile { int expectedNumLines = width * height; int currentNumLines = 0; // Counter to keep track number of lines while ((line = br.readLine()) != null) { currentNumLines++; currentNumLines += 1; } if (expectedNumLines != currentNumLines) { System.out.println("Error: Expected " + expectedNumLines + " lines, but found " + currentNumLines + " lines."); Loading @@ -41,9 +44,8 @@ public class AcceptFile { } // Check that each of the following lines has exactly 3 integers between 0 and 100 for (int i = 0; i < 2 + expectedNumLines; i++) { // We skip the first two lines br.readLine(); } br.reset(); // puts us back to line 3 and no longer need to skip first two lines while ((line = br.readLine()) != null) { String[] tokens = line.split("\\s+"); if (tokens.length != 3) { Loading @@ -59,8 +61,6 @@ public class AcceptFile { } } br.close(); // When text file meets all the requirements System.out.println("The file \"" + fileName + "\" meets all the requirements."); } catch (IOException e) { Loading Loading
AcceptFile.java +7 −7 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ import java.io.IOException; public class AcceptFile { public static void main(String[] args) { String fileName = "wrongInput.txt"; // Replace with the name of text file to check String fileName = "correctInput.txt"; // Replace with the name of text file to check // BufferedReader is used to parse through text file line by line try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { Loading @@ -23,6 +23,9 @@ public class AcceptFile { line = br.readLine(); int height = Integer.parseInt(line.trim()); //to save the point in the file after reading width and height (line 3) br.mark(1000); if (width <= 0 || height <= 0) { System.out.println("Error: width and height must be positive integers."); return; Loading @@ -33,7 +36,7 @@ public class AcceptFile { int expectedNumLines = width * height; int currentNumLines = 0; // Counter to keep track number of lines while ((line = br.readLine()) != null) { currentNumLines++; currentNumLines += 1; } if (expectedNumLines != currentNumLines) { System.out.println("Error: Expected " + expectedNumLines + " lines, but found " + currentNumLines + " lines."); Loading @@ -41,9 +44,8 @@ public class AcceptFile { } // Check that each of the following lines has exactly 3 integers between 0 and 100 for (int i = 0; i < 2 + expectedNumLines; i++) { // We skip the first two lines br.readLine(); } br.reset(); // puts us back to line 3 and no longer need to skip first two lines while ((line = br.readLine()) != null) { String[] tokens = line.split("\\s+"); if (tokens.length != 3) { Loading @@ -59,8 +61,6 @@ public class AcceptFile { } } br.close(); // When text file meets all the requirements System.out.println("The file \"" + fileName + "\" meets all the requirements."); } catch (IOException e) { Loading