2

I have read in a text file and am scanning said file. The question I have is how would I skip over lines that include a certain character (in my case lines that start with " // " and " " (whitespace).

Here is my code at the moment. Can someone point me in the right direction?

    File dataFile = new File(filename);
    Scanner scanner = new Scanner(dataFile);


      while(scanner.hasNext())
      {
         String lineOfText = scanner.nextLine();
         if (lineOfText.startsWith("//")) {
           System.out.println(); // not sure what to put here
         }
      System.out.println(lineOfText);
      }
   scanner.close();
Luke G
  • 25
  • 1
  • 1
  • 3

5 Answers5

1

You will only want to execute the code within the while-loop if the line of text doesn't start with a / or whitespace. You can filter these out as seen below:

while(scanner.hasNext()) {
   String lineOfText = scanner.nextLine();
   if (lineOfText.startsWith("//") || lineOfText.startsWith(" ")) {
      continue; //Exit this iteration if line starts with space or /
   }
   System.out.println(lineOfText);
}
Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38
  • I will later have to read the data into the fields of other classes using a second scanner. Will it not read the data with the lines that I wish to be ignored? – Luke G Mar 09 '17 at 02:56
  • There are no fields being read into in your question. If you want to do something with the lineOfText, then include it in the if-statement. Otherwise the line will just be skipped as I assume you require. – Matt Goodrich Mar 09 '17 at 02:58
  • I feel as if I should have worded the question a bit better. My bad. I want to completely ignore the lines with "//" and white space, as I will be using the actual data later for storing in arrays. – Luke G Mar 09 '17 at 03:02
  • In this case you will want to use continue. I have edited my answer, and no problem! – Matt Goodrich Mar 09 '17 at 03:05
  • 1
    Thanks for the swift answers! – Luke G Mar 09 '17 at 03:07
1

As you are iterating over the lines of text in the file, use String's startsWith() method to check if the line starts with the sequences you are trying to avoid.

If it does, continue to the next line. Otherwise, print it.

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

    if (lineOfText.startsWith("//") || lineOfText.startsWith(" ") ) {
        continue;
    }

    System.out.println(lineOfText);
}
Jameson
  • 6,400
  • 6
  • 32
  • 53
Nitin Bohra
  • 112
  • 3
  • please add an explanation for your code - [From Review](http://stackoverflow.com/review/low-quality-posts/15465052) – Suraj Rao Mar 09 '17 at 05:16
0

Just use a continue like -

if (lineOfText.startsWith("//")) {
  continue; //would skip the loop to next iteration from here
}

Detials - What is the "continue" keyword and how does it work in Java?

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353
0

If you're just interested in printing out the lines of code that begin with a "//" then you should just use the continue keyword in java.

String lineOfText = scanner.nextLine();
if (lineOfText.startsWith("//")) {
    continue;
}

See this post for more information regarding the "continue" keyword.

Community
  • 1
  • 1
bennerv
  • 86
  • 3
0

You can just insert "else" in your code like:

public static void main(String[] args) throws FileNotFoundException {

     File dataFile = new File("testfile.txt");
        Scanner scanner = new Scanner(dataFile);


          while(scanner.hasNext())
          {
             String lineOfText = scanner.nextLine();
             if (lineOfText.startsWith("//")) {
               System.out.println(); 
             }
             else
                 System.out.println(lineOfText);
          }
       scanner.close();
}

}

bpp
  • 1