Euriloco
Euriloco

Reputation: 253

Console input and ENTER key with Java

I'm learning Java with the book: Java. A begginer's guide. The book shows the following example:

// Guess the letter game, 4th version.
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: ");

            // read a character
            ch = (char) System.in.read();

            // discard any characters in the input buffer
            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);
    }
}

Here is a sample run:

I'm thinking of a letter between A and Z.
Can you guess it: a
...Sorry, you're too high
Try again!

I'm thinking of a letter between A and Z.
Can you guess it: europa
...Sorry, you're too high
Try again!

I'm thinking of a letter between A and Z.
Can you guess it: J
...Sorry, you're too low
Try again!

I'm thinking of a letter between A and Z.
Can you guess it:

I think the output of the program should be:

I'm thinking of a letter between A and Z. 
Can you guess it: a...Sorry, you're too high 
Try again! 

Without a \n between 'a' and '...Sorry, you are too high'. I don't know why apears a new line. The do-while erases it. Thank you.

Upvotes: 0

Views: 2998

Answers (2)

Binkan Salaryman
Binkan Salaryman

Reputation: 3058

Instead doing stuff char-by-char, you could easily utilize a Scanner:

replace

// read a character
ch = (char) System.in.read();

// discard any characters in the input buffer
do {
    ignore = (char) System.in.read();
} while (ignore != '\n');

with

Scanner in = new Scanner(System.in); //outside your loop
while(true) {
    String input = in.nextLine();
    if(!input.isEmpty()) {
        ch = input.charAt(0);
        break;
    }
}

Upvotes: 0

Uma Kanth
Uma Kanth

Reputation: 5629

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

actually reads a single character.

if the input is - a\n only the first character is read and stored in ch. which is a in this case.

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

This is used to remove any unwanted characters.

Why did they use this?

We just need a single letter.

So if the user had given an input which is not a single character, like "example" and if your code didn't have the loop check.

First the ch becomes e, then x ....so on.

Even without the user entering a alphabet the previous input is considered to be entered.

what if only Enter(\n) was pressed

As even \n is considered a character it is also read. In the comparison the ASCII value of it is considered.

Have a look at this question. In which a user didn't check for the unnecessary characters and got an unexpected output.

Upvotes: 1

Related Questions