Search code examples
javaend-of-line

How to parse end of line string in Java


I would like to read a text file which has some strings such as:

abc  
def

ghi  
jkl

mno  
pqr

I am using a while loop to read only a pair and store them in an arraylist. I would like to stop as soon I encounter an empty line (\n) e.g. after def. But my code wont work ... what am I doing wrong?

while(!"\n".equals(sCurrentLine)) {
     -- do stuff --
}

Solution

  • If you are reading line by line then empty line is empty line (""), not "\n" which is string of length 1

    You may also need check that there's no more lines in this case BufferedReader.readLine() returns null