title: How does input.nextInt() work exactly?
body: <p>This is the program</p>
  
  <pre><code>public class bInputMismathcExceptionDemo {
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      boolean continueInput = true;
      do {
          try {
              System.out.println("Enter an integer:");
              int num = input.nextInt();
  
              System.out.println("the number is " + num);
              continueInput = false;
          }
          catch (InputMismatchException ex) {
              System.out.println("Try again. (Incorrect input: an integer is required)");
          } 
          input.nextLine();
      }   
      while (continueInput);
  
  }
  }
  </code></pre>
  
  <p>I know <code>nextInt()</code> only read the integer not the <code>"\n"</code>, but why should we need the <code>input.nextLine()</code> to read the <code>"\n"</code>? is it necessary?? because I think even without <code>input.nextLine()</code>, after it goes back to <code>try {}</code>, the <code>input.nextInt()</code> can still read the next integer I type, but in fact it is a infinite loop.</p>
  
  <p>I still don't know the logic behind it, hope someone can help me.</p>