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

久未见 提交于 2019-11-29 17:17:55

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.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!