1
class Noob1 {
    public static void main(String args[]) 
        throws java.io.IOException{

        char ch, answer;
        answer = 'g';

        do {
            System.out.println("Guess from A-L:");
            ch = (char) System.in.read();
                if(ch == answer){
                    System.out.println("Congratz!");
            } else System.out.println("Try again!");
        } while (ch != answer);
    }
}

The output is this:

Guess from A-L:
a
Try again!
Guess from A-L:
Try again!  // this is where intuitively it should ask for input again
Guess from A-L:
Try again!
Guess from A-L:
g           //  input is skipped a few times
Congratz!

I cannot figure out why, I'm reading a book for beginners and everything should've been covered, what am I missing?

Adding the code below at the end of the loop confirms that it is being looped but the input is somehow skipped.

i++;
System.out.println(i);

So I got it working using this:

import java.io.*;

class Noob1 
{
    public static void main(String args[]) throws java.io.IOException
    {
        char ch, answer;
        String tmp;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        answer = 'g';

            do 
            {
                System.out.println("Guess character followed by ENTER:");
                tmp = bufferedReader.readLine();
                ch = tmp.charAt(0); // only first char is considered
                if (ch == answer) 
                    System.out.println("Gratz! the answer was: " + ch + "!");
                else 
                    System.out.println("Nope, try again..");
            } while (ch != answer);
    }
}

Is the formatting still terrible?

naike
  • 11
  • 3
  • It's useful to get in the habit of using consistent indentation and bracing right from the beginning. The code in the question could use some cleanup. – T.J. Crowder Jun 05 '16 at 15:48
  • @Tunaki: The OP isn't using `Scanner`. But there may well still be an earlier version of this question using `System.in` directly. – T.J. Crowder Jun 05 '16 at 15:49
  • 1
    @T.J.Crowder You are correct... silly me. Thanks for pointing this out. This would be more appropriate http://stackoverflow.com/questions/27239265/whats-wrong-with-for-loop or http://stackoverflow.com/questions/15296637/system-in-read-miss-execution – Tunaki Jun 05 '16 at 15:52
  • @Tunaki: Excellent finds, I used the first one. – T.J. Crowder Jun 05 '16 at 15:54

2 Answers2

3

read only reads a single character, but when answering, you're typing more than one character: You're typing a followed by a newline, because console input doesn't get sent to the stream until you press Enter. That newline sits there in the stream waiting to be read by the next read, which is why it doesn't wait for you to type something again.

You may want to wrap System.in in a BufferedReader and use BufferedReader#readLine and handle the fact you get a string instead of one character, or alternately after getting your character, call read repeatedly until you get the newline. I'd opt for bufferedReader.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Ah, that was briefly mentioned in somewhere. Why does it loop through 3 times though? Doesn't a newline have a single value or why does it take up 3 buffer "spaces"? How would I use bufferedReader in this example? – naike Jun 05 '16 at 15:58
  • 1
    @naike: On some OS's (such as Windows), Enter = `\r\n`, which is two characters (carriage return [char 13] and newline [char 10]). Re `BufferedReader`, look through the JavaDoc and give it a go, again being aware of the fact you'll get a string instead of a char. If you run into trouble after spending some time on it, post a new question with your `BufferedReader` code. – T.J. Crowder Jun 05 '16 at 16:00
  • 1
    Thanks, that cleared some things up, I'll give it a go. – naike Jun 05 '16 at 16:06
  • Got it working, see first post. Thanks. – naike Jun 05 '16 at 16:32
  • @naike: Updating a question like that isn't really something you should do on SO. Side note: There's no reason whatsoever to recreate your `BufferedReader` on each loop. – T.J. Crowder Jun 05 '16 at 16:33
  • Ok, thanks, thought someone else might have the same problem. I'll leave it at that. – naike Jun 05 '16 at 16:47
1

You are entering 2 characters at once - the character itself and a '\n' new line character. Replace your implementation of reading the character with this :

Scanner s= new Scanner(System.in);
char x = s.next().charAt(0);

charAt makes sure only first character is extracted.

Jaydev
  • 1,545
  • 18
  • 36