-2

I am trying to read data from a .txt file, i need to be ignoring any line that starts with a // or a blank line but i can't seem to get the delimiter to work correctly.

public void readVehicleData()
throws FileNotFoundException
{
    FileDialog fileBox = new FileDialog(myFrame, "Open", FileDialog.LOAD);
    fileBox.setVisible(true);

    String filename = fileBox.getFile();
    File vehicleData = new File(filename);

    Scanner scanner = new Scanner(vehicleData).useDelimiter("\"(,\")?");


    while( scanner.hasNext() )
    {
        String lineOfText = scanner.nextLine();

        System.out.println(lineOfText);

    }
    scanner.close();
}

This is the .txt file i am trying to read:

// this is a comment, any lines that start with //
// (and blank lines) should be ignored

AA, TF-63403, MJ09TFE, Fiat 
A, TF-61273, MJ09TFD, Fiat
A, TF-64810, NR59GHD, Ford
B , TF-68670,MA59DCS, Vauxhall
B, TF-61854,  MJ09TFG, Fiat
B, TF-69215, PT09TAW, Peugeot
C, TF-67358, NR59GHM, Ford
acarlstein
  • 1,799
  • 2
  • 13
  • 21
calumY
  • 1
  • Read a line at a time with `BufferedReader` or any type of `Reader` and just ignore lines that fulfill those conditions. – Vucko Apr 18 '19 at 22:33
  • 1
    Use a BufferedReader to read line by line. For each line, test if it's blank or if it starts with "//". The javadoc of BufferedReader and of String are your friends. – JB Nizet Apr 18 '19 at 22:33
  • Possible duplicate of [How do I create a Java string from the contents of a file?](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – Vucko Apr 18 '19 at 22:37
  • If I answered your question, please accept it. :) – Nirup Iyer Apr 18 '19 at 22:56

1 Answers1

1

If I understand your question properly, you do not need to specify a delimiter.

Scanner scanner = new Scanner(vehicleData);


while( scanner.hasNext() )
{
    String lineOfText = scanner.nextLine();
    if(lineOfText.length() == 0 || lineOfText.startsWith("//"))
            continue;
    System.out.println(lineOfText);

}
scanner.close();
Nirup Iyer
  • 1,215
  • 2
  • 14
  • 20