title: checking if input from scanner is int with while loop java
body: <p>I basically want the following while loop to check if the input is an integer. It cannot contain decimals because it is pointing to an array. If the value entered is a decimal it should prompt the user again. Problem is I get two prompts before the while loop starts with this code. Any ideas?</p>
  
  <pre><code>      System.out.print("Enter month (valid values are from 1 to 12): ");
      Scanner monthScan = new Scanner(System.in);
      int monthInput = monthScan.nextInt();
      // If the month input is below 1 or greater than 12, prompt for another value
      while(monthInput&lt;1 || monthInput&gt;12 || !monthScan.hasNextInt())
      {
          System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");
          monthInput = new Scanner(System.in).nextInt();
      }
  </code></pre>
  
  <p>thanks</p>
  
  <p>EDIT: The current output gives the following:</p>
  
  <pre><code>Enter month (valid values are from 1 to 12): 2
  2
  </code></pre>
  
  <p>Notice how I had to enter 2 twice even though it is a valid value.</p>