<p>I am using the code snippet below, however it's not working quite as I understand it should.    </p>

<pre><code>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();
    }
}
</code></pre>

<p>From reading the Javadoc about <code>readLine()</code> it says: </p>

<p>Reads a line of text. A line is considered to be terminated by any one of a line feed (<code>\n</code>), a carriage return (<code>\r</code>), or a carriage return followed immediately by a linefeed. </p>

<p><strong>Returns</strong>:
A <code>String</code> containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached </p>

<p><strong>Throws</strong>: 
<code>IOException</code> - If an I/O error occurs</p>

<p>From my understanding of this, <code>readLine</code> should return null the first time no input is entered other than a line termination, like <code>\r</code>. However, this code just ends up looping infinitely. After debugging, I have found that instead of null being returned when just a termination character is entered, it actually returns an empty string (""). This doesn't make sense to me. What am I not understanding correctly?</p>

[{'answer_id': 25036, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>No input is not the same as the end of the stream. You can usually simulate the end of the stream in a console by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> (AFAIK some systems use <kbd>Ctrl</kbd>+<kbd>Z</kbd> instead). But I guess this is not what you want so better test for empty strings additionally to null strings.</p>\n'}, {'answer_id': 25043, 'author': 'Tom Lokhorst', 'author_id': 2597, 'author_profile': 'https://Stackoverflow.com/users/2597', 'pm_score': 4, 'selected': True, 'text': '<blockquote>\n  <p>From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like \'\\r\'.</p>\n</blockquote>\n\n<p>That is not correct. <code>readLine</code> will return <code>null</code> if the end of the stream is reached. That is, for example, if you are reading a file, and the file ends, or if you\'re reading from a socket and the socket closses.</p>\n\n<p>But if you\'re simply reading the console input, hitting the return key on your keyboard does not constitute an end of stream. It\'s simply a character that is returned (<code>\\n</code> or <code>\\r\\n</code> depending on your OS).</p>\n\n<p>So, if you want to break on both the empty string and the end of line, you should do:</p>\n\n<pre><code>while (line != null &amp;&amp; !line.equals(""))\n</code></pre>\n\n<p>Also, your current program should work as expected if you pipe some file directly into it, like so:</p>\n\n<pre><code>java -cp . Echo &lt; test.txt\n</code></pre>\n'}, {'answer_id': 36438, 'author': 'Bartosz Bierkowski', 'author_id': 3666, 'author_profile': 'https://Stackoverflow.com/users/3666', 'pm_score': 1, 'selected': False, 'text': '<p>There\'s a nice <a href="http://commons.apache.org/lang/" rel="nofollow noreferrer">apache commons lang</a> library which has a good api for common :) actions. You could use statically import StringUtils and use its method isNotEmpty(String ) to get:</p>\n\n<pre><code>while(isNotEmpty(line)) {\n    System.out.println(line);\n    line = br.readLine();\n}\n</code></pre>\n\n<p>It might be useful someday:) There are also other useful classes in this lib.</p>\n'}]
['https://Stackoverflow.com/questions/25033', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2628/']