I’ve been having lots of trouble trying to get either a scanner or a buffered reader to try and detect a blank line. For example if I have a file that contains:
there
cat
dog
(BLANK LINE)
If I do this:
while( scan.hasNextLine() )
{
String line = scan.nextLine();
...
...
}
The scanner doesn’t pick up the blank line. I tried to use a buffered reader also but I run into this issue. Is there some way the scanner can just return a “” whenever it finds a blank line like that? Cheers
Your input has as many lines as it has
\ncharacters. Given the inputthe next-lines will be correctly divided as
(In other words, there is no fourth blank line, since it is not terminated by a
\n.)Put differently, after the
"dog\n"has been read, the scanner (or buffered reader for that matter) has reached EOF and there’s not even an empty line to return. (Note that when the lines are returned, the new-line character is stripped off.)So, since this is the expected behavior, I don’t know what the easiest fix is. I suspect that the best way to solve this is simply to append a
\nto the input, so that the loop runs an extra iteration.