title: How can I clear the Scanner buffer in Java?
body: <p>I have something like this:</p>
  
  <pre><code>    Scanner in=new Scanner(System.in);
      int rounds = 0;
      while (rounds &lt; 1 || rounds &gt; 3) {
          System.out.print("How many rounds? ");
          if (in.hasNextInt()) {
              rounds = in.nextInt();
          } else {
              System.out.println("Invalid input. Please try again.");
              System.out.println();
          }
          // Clear buffer
      }
      System.out.print(rounds+" rounds.");
  </code></pre>
  
  <p>How can I clear the buffer?</p>
  
  <p>Edit: I tried the following, but it does not work for some reason:</p>
  
  <pre><code>while(in.hasNext())
      in.next();
  </code></pre>