title: Java: Infinite loop using Scanner in.hasNextInt()
body: <p>I am using the following code:</p>
  
  <pre><code>while (invalidInput)
  {
      // ask the user to specify a number to update the times by
      System.out.print("Specify an integer between 0 and 5: ");
  
      if (in.hasNextInt())
      {
          // get the update value
          updateValue = in.nextInt();
  
          // check to see if it was within range
          if (updateValue &gt;= 0 &amp;&amp; updateValue &lt;= 5) 
          { 
              invalidInput = false; 
          } 
          else 
          {
              System.out.println("You have not entered a number between 0 and 5. Try again.");
          }
      } else
      {
          System.out.println("You have entered an invalid input. Try again.");
      }
  }
  </code></pre>
  
  <p>However, if I enter a 'w' it will tell me "You have entered invalid input. Try Again." and then it will go into an infinite loop showing the text "Specify an integer between 0 and 5: You have entered an invalid input. Try again."</p>
  
  <p>Why is this happening? Isn't the program supposed to wait for the user to input and press enter each time it reaches the statement:</p>
  
  <pre><code>if (in.hasNextInt())
  </code></pre>