Siraphob K.
Siraphob K.

Reputation: 57

What is a line buffered in Java?

I'm having a little problems about Java input. My code is a guessing letter game.(I read it from a book.) Here's my code.

public class Guess4 {
    public static void main(String args[])
    throws java.io.IOException{
        char ch, ignore, answer = 'K';

        do{
            System.out.println("I'm thinking of a letter between A and Z.");
            System.out.print("Can you guess it : ");

            ch = (char) System.in.read();

            do{
                ignore = (char) System.in.read();
            }while(ignore != '\n');

            if(ch == answer) System.out.println("** Right **");
            else{
                System.out.print("...Sorry, you're ");
                if(ch<answer) System.out.println("too low.");
                else System.out.println("too high.");
                System.out.println("Try again!\n");
            }
        }while(answer != ch);
    }

I do not understand why this block of codes is necessary.

do{
    ignore = (char) System.in.read();
}while(ignore != '\n');

When this block is deleted. The output came out differently.

The book did said something about the line buffered but I still don't understand. Can anyone explain what a line buffered is?

Thank you.

Upvotes: 1

Views: 1475

Answers (4)

prateeknischal
prateeknischal

Reputation: 792

Judging by the code I suppose the input of the following code would be something like :

A
J
K

So at the third attempt it would print Success. But if you look at the input stream (as the system represents it) it looks like : A\nJ\nK\n . Now your code goes through the input stream to find a valid input and reads all the characters after the first one till the \n character as junk i.e. ignores it.

So if the input stream is like AK\nKA\n it will print success at the second attempt and not at the first as it ignores all characters after 1 character of input i.e. A for the first and K for the second line.

If you remove the block you mentioned, then your code reads all the characters in the input stream including \n which would obviously not match to K.

A buffered input stream processes the input removing any extra junk characters for you to just read the input. eg: BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

This passes the System.in stream to an Reader object and BufferedReader does all the magic. There is much more to these classes once you get into a little more depth.

Upvotes: 0

LH NCL
LH NCL

Reputation: 34

The program is asking the user to enter a single letter. But System.in is buffered. The user can actually type several characters. Even a long sentence. Until finally he presses the Enter, which is character '\n'.

The program reads a character into ch:

ch = (char) System.in.read();

Then it goes further, and keeps reading until it finds a '\n' character:

   do {
        ignore = (char) System.in.read();
    } while (ignore != '\n');

If you don't do this step, then any extra input the user entered will get carried over for the next iteration of the outer loop. You don't want anything carried over, you want just the first character entered, discard the rest, so that in the next iteration you start with a clean buffer.

Try the code with the loop with ignore commented out. If you enter a single letter, the outer loop will run twice, for the letter you entered and the '\n' character that ended your input. If you enter multiple letters, let's say "hello", the loop will run 6 times, 5 times for the letters h-e-l-l-o and one more time for the '\n' character that ends input.

-> The loop with ignore is necessary to clear the input buffer.

Upvotes: 2

Kayaman
Kayaman

Reputation: 73548

It's the nextline character, that comes from pressing the enter key. If you're to type a letter (let's say A) and press enter, the characters that you get from System.in will be A\n (2 characters).

The \n is then removed from the input buffer so it doesn't mess up the logic of the program.

You're probably reading an old book, since newer ones tend to use the Scanner class which is simpler than reading from System.in. You might want to consider some other reading material, since reading an outdated book can result in some funny ideas.

Upvotes: 0

Henry
Henry

Reputation: 43728

When you enter text via the console the input will typically be sent to the program only when you press "Enter" or "Return". The program will then get the full line (including the newline character \n marking the end of the line) and not only the one character you are interested in.

The while loop deals with these extra characters and throws them away.

Upvotes: 0

Related Questions