How can i detect BlankLine that Scanner has receive?

亡梦爱人 提交于 2019-12-07 01:56:27

You can implement custom function like below which will return you nextLine if it is not empty.

 public static String skipEmptyLines(Scanner fileIn) {
    String line = "";
    while (fileIn.hasNext()) {
        if (!(line = fileIn.nextLine()).isEmpty()) {
            return line;
        }
    }
    return null;
}

You can simply check your scanner.nextLine() for a Newline "\n" (I mean "", because nextLine() does not read "\n" at the end of any line).. If its equal, it will be a blank line..

if (scanner.nextLine().equals("")) {
    /** Blank Line **/
}

BTW, there is a problem with your code: -

while(fileIn.hasNextLine()){          
         String line = fileIn.nextLine();
         String linename = fileIn.nextLine();
         String statusline = fileIn.nextLine();

You are assuming that your fileIn.hasNextLine() will confirm about the next three lines being not null.

Everytime you do a fileIn.nextLine() you need to check whether it's available or not.. Or you will get exception...

*EDIT: - O.o... I see there you have handled exception.. Then there will be no problem.. But still you should modify the above code.. It doesn't look pretty..

After checking for an existing line with scanner.hasNextLine() method, you can use this condition:

String line = null;
if((line = scanner.nextLine()).isEmpty()){
   //your logic when meeting an empty line
}

and use the line variable in your logic.

Try this...

while(scanner.hasNextLine()){

    if(scanner.nextLine().equals("")){

            // end of profile one...

       }

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