0

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.

2
  • Change book :) - Use : Head First Java - oReally Commented Jul 3, 2015 at 10:20
  • 1
    \n is also considered as a character, so we need to skip it in a letter game and hence the do while loop Commented Jul 3, 2015 at 10:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. But 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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.