diff --git a/src/main/io/DataFileReader.java b/src/main/io/DataFileReader.java index 2f49a5c..ad9585c 100644 --- a/src/main/io/DataFileReader.java +++ b/src/main/io/DataFileReader.java @@ -32,8 +32,13 @@ public void close() { private String nextLine() { String nextLine = fileScanner.nextLine().trim(); - if (commentStr != null) - nextLine = nextLine.split(commentStr)[0].trim(); + if (commentStr != null) { + if (nextLine.equals(commentStr)) { + nextLine = ""; + } else { + nextLine = nextLine.split(commentStr)[0].trim(); + } + } // Since blank lines are ignored if (nextLine.length() == 0) diff --git a/test/main/io/DataFileReaderTest.java b/test/main/io/DataFileReaderTest.java index 1feb314..6a97cd2 100644 --- a/test/main/io/DataFileReaderTest.java +++ b/test/main/io/DataFileReaderTest.java @@ -18,26 +18,28 @@ public void readWithComments() throws IOException { File tempFile = new File("test/test_data/dataFileTest.cfd"); try (FileWriter writer = new FileWriter(tempFile)) { - String textWithComments = "% Comment line\n" + - "int_param1=56\n" + - "IntParam2 = 85\n" + - "DoubleParam1 = 2452 % This a double param\n" + - "str1 = Just a plain line\n" + - "987 34 76\n" + - "45 72 25 98 41 23 14 57 92 12 % int array\n" + - "line=empty"; + String textWithComments = """ + % Comment line + int_param1=56 + IntParam2 = 85 + DoubleParam1 = 2452 % This a double param + str1 = Just a plain line + 987 34 76 + 45 72 25 98 41 23 14 57 92 12 % int array + line=empty"""; writer.write(textWithComments); } - DataFileReader fileReader = new DataFileReader(tempFile, "%"); + try (DataFileReader fileReader = new DataFileReader(tempFile, "%")) { - assertEquals(56, fileReader.readIntParameter("int_param1")); - assertEquals(85, fileReader.readIntParameter("IntParam2")); - assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15); - assertEquals("Just a plain line", fileReader.readParameter("str1")); - assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15); - assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray()); + assertEquals(56, fileReader.readIntParameter("int_param1")); + assertEquals(85, fileReader.readIntParameter("IntParam2")); + assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15); + assertEquals("Just a plain line", fileReader.readParameter("str1")); + assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15); + assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray()); - assertThrows(IllegalArgumentException.class, () -> fileReader.readParameter("Dummy")); // line=empty + assertThrows(IllegalArgumentException.class, () -> fileReader.readParameter("Dummy")); // line=empty + } if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile); } @@ -47,22 +49,54 @@ public void readWithoutComments() throws IOException { File tempFile = new File("test/test_data/dataFileTest.cfd"); try (FileWriter writer = new FileWriter(tempFile)) { - String textWithoutComments = "int_param1=56\n" + - "IntParam2 = 85\n" + - "DoubleParam1 = 2452 \n" + - "str1 = Just a plain line\n" + - "987 34 76\n" + - "45 72 25 98 41 23 14 57 92 12\n"; + String textWithoutComments = """ + int_param1=56 + IntParam2 = 85 + DoubleParam1 = 2452\s + str1 = Just a plain line + 987 34 76 + 45 72 25 98 41 23 14 57 92 12 + """; writer.write(textWithoutComments); } - DataFileReader fileReader = new DataFileReader(tempFile); - - assertEquals(56, fileReader.readIntParameter("int_param1")); - assertEquals(85, fileReader.readIntParameter("IntParam2")); - assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15); - assertEquals("Just a plain line", fileReader.readParameter("str1")); - assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15); - assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray()); + try (DataFileReader fileReader = new DataFileReader(tempFile)) { + + assertEquals(56, fileReader.readIntParameter("int_param1")); + assertEquals(85, fileReader.readIntParameter("IntParam2")); + assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15); + assertEquals("Just a plain line", fileReader.readParameter("str1")); + assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15); + assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray()); + } + + if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile); + } + + @Test + public void file_with_only_comment_on_a_line() throws IOException { + File tempFile = new File("test/test_data/dataFileTest.cfd"); + + try (FileWriter writer = new FileWriter(tempFile)) { + String textWithoutComments = """ + % + int_param1=56 + IntParam2 = 85 + DoubleParam1= 2452\s + str1 = Just a plain line + 987 34 76 + 45 72 25 98 41 23 14 57 92 12 + """; + writer.write(textWithoutComments); + } + try (DataFileReader fileReader = new DataFileReader(tempFile, "%")) { + + assertEquals(56, fileReader.readIntParameter("int_param1")); + assertEquals(85, fileReader.readIntParameter("IntParam2")); + assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15); + assertEquals("Just a plain line", fileReader.readParameter("str1")); + assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15); + assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray()); + } if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile); }