Why empty lines are being printed at the when I use BufferedReader to read characters from the console and print them?

Viewed 43

Here is my program.

// Here we use a BufferredReader to read characters from console.

package fileIO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BRRead {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        char ch;
        
        System.out.println("Enter characters, 'q' to quit.");
        do {
            ch = (char)br.read(); 
            System.out.println(ch);
        } while(ch!='q');

    }

}

Input 1: testq

Output:

t

e

s

t

q

Input 2: test

Output:

t

e

s

t

""

""

""

""

where "" means empty line. My question is why 4 empty lines are printed for the case when the input characters doesn't contain letter 'q' but aren't printed when the input characters contain letter 'q'?

1 Answers

Complete-er answer at the bottom

Haven't run it at all to check, though it seems like it could have something to do with the use of a do-while loop instead of a while loop

This format will make sure that the character 'q' is not read before it attempts to output anything

while(ch!='q')
{
    System.out.println(ch);
    ch = (char)br.read(); 
} 

And this format prints the read character before testing if it is valid

do {
    ch = (char)br.read(); 
    System.out.println(ch);
} while(ch!='q');

EDIT after some trial/error

With this version, I have used the Scanner class which is quite similar, and I am more familiar with it. It may go something like so:

  • Create Object(s) for reading the data BufferedReader,Scanner,etc.
  • Check if there is content present
  • Accept the data as a String and read the first character
  • Output if the String is is not q
  • Cycle the test-> read -> print -> loop, till q is entered
Scanner input = new Scanner(System.in);
System.out.println("Enter characters, 'q' to quit.");
String read = input.nextLine();
while(read.length() <= 1 && read.charAt(0) != 'q')
{
    System.out.print(read);
    read = input.nextLine();
}

The (almost) original method*

Eureaka!

  • name.read() Reads a single character -- However, this returns an int datatype which cannot be converted with the (char) mask.
  • name.readLine() Reads a line of text -- With this, you can simply take the character at index 0

https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html

do {
    full = br.readLine(); 
    System.out.print(full);
} while(full.length() <= 1) && full.charAt(0) != 'q');

Not sure what the best way to do it would be between a do-while and a while loop, and that may ultimately come down to use case and opinion. and you may want to make a boolean method too

// true if String matches 'q' or "quit"
public static boolean isQuit(input)
{
    String lowercase = input.toLower();
    char start = input.charAt(0);
    return (lowercase.equals("quit") 
           || (input.length() <= 1 && start == 'q'));
}
Related