Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I Am Not Getting the Result I Expect Using readLine() in Java

Tags:

java

java-io

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

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

From reading the Javadoc about readLine() it says:

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

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

Throws: IOException - If an I/O error occurs

From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like \r. 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?

like image 905
Aaron Avatar asked Aug 24 '08 13:08

Aaron


People also ask

How to use ReadLine in Java?

The readLine() method of Console class in Java is used to read a single line of text from the console. Parameters: This method does not accept any parameter. Return value: This method returns the string containing the line that is read from the console. It returns null if the stream has ended.

Is ReadLine terminated by EOF?

The readline() method doesn't trigger the end-of-file condition. Instead, when data is exhausted, it returns an empty string.

Why does ReadLine read newline?

The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end.

What is the difference between read and ReadLine in Java?

While Read() and ReadLine() both are the Console Class methods. The only difference between the Read() and ReadLine() is that Console. Read is used to read only single character from the standard output device, while Console. ReadLine is used to read a line or string from the standard output device.


1 Answers

From my understanding of this, readLine should return null the first time no input is entered other than a line termination, like '\r'.

That is not correct. readLine will return null 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.

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 (\n or \r\n depending on your OS).

So, if you want to break on both the empty string and the end of line, you should do:

while (line != null && !line.equals(""))

Also, your current program should work as expected if you pipe some file directly into it, like so:

java -cp . Echo < test.txt
like image 200
Tom Lokhorst Avatar answered Sep 20 '22 22:09

Tom Lokhorst