I am using the code snippet below, however it does not work as I understand it.
public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; try { line = br.readLine(); while(line != null) { System.out.println(line); line = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } }
Reporting to Javadoc about readLine() , he says:
Reads text. A line is considered to be completed by any of the lines ( \n ), carriage return ( \r ) or carriage return, which the line immediately follows.
Returns : A String containing the contents of the string, not including end-of-line characters, or null if end of stream is reached
Throws : IOException - If an I / O error has occurred
From my understanding of this, readLine should return null the first time that input is not entered other than line termination, such as \r . However, this code simply ends endlessly. After debugging, I found that instead of null, returned when you enter only the termination character, it actually returns an empty string (""). It makes no sense to me. What I do not understand correctly?
Aaron source share