title: JAVA: How can a nested do-while loop discard characters from the input buffer?
body: <p>I'm working my way through Oracle's "Java: A Beginner's Guide" and I am stumped by a subsection about using nested do-while loops. The example given creates a simple guessing game where the user inputs a letter between A and Z. If he guesses correctly the program returns "Right", otherwise additional code is executed and a hint is given -- it either returns "too high" or "too low".</p>
  
  <p>The author states that he is using a nested do-while loop to "discard any other characters in the input buffer". </p>
  
  <p>When I run the program without the nested do-while, whether I input a character greater than the answer that is being searched for or less than the answer that is being searched for, the program always evaluates it as being less than the answer. When I run the program with the nested do-while, the program runs correctly.</p>
  
  <p>My Question:
  I don't understand how the nested do-while is affecting the rest of the program. What exactly is the nested do-while doing that the outer do-while isn't?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>class Application {
      public static void main(String[] args)
      throws java.io.IOException {
  
          char ch, ignore, answer = 'K';
  
          do {
              System.out.println("I'm thinking of a number between A and Z");
              System.out.println("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.println("Sorry, you're ");
                  if(ch &lt; answer) System.out.println("too low.");
                  else System.out.println("too high.");
                  System.out.println("Try again!\n");
              }
          } while(answer != ch);
      }
  }
  </code></pre>