Java - Scanner Class - Skipping over the first line when reading a textfile

后端 未结 2 1088
甜味超标
甜味超标 2020-12-20 04:09

When using a Scanner object to read from a textfile, I want it to skip over the very first line in the file. How would I do achieve this?

相关标签:
2条回答
  • 2020-12-20 04:18

    Just use file.nextLine() before your while loop. This will skip the first line, as explained in the JavaDoc.

    And a note about your naming. The Java language has widely accepted conventions. Class Names always start with an upper case letter, and variable names always start with a lower case letter (except constants, but don't worry about that now). Read more here.

    0 讨论(0)
  • 2020-12-20 04:20

    Add the below code before the while loop to skip the first line.

    if(file.hasNext()==true)
    {
       file.nextLine();
    }
    else
    {
        System.out.println("Error: File is empty");
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题