title: How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner
body: <p>So, I'm getting stuck with this piece of code:</p>
  
  <pre><code>import java.util.InputMismatchException;
  import java.util.Scanner;
  
  public class ConsoleReader {
  
      Scanner reader;
  
      public ConsoleReader() {
          reader = new Scanner(System.in);
          //reader.useDelimiter(System.getProperty("line.separator"));
      }
  
      public int readInt(String msg) {
          int num = 0;
          boolean loop = true;
  
          while (loop) {
              try {
                  System.out.println(msg);
                  num = reader.nextInt();
  
                  loop = false;
              } catch (InputMismatchException e) {
                  System.out.println("Invalid value!");
              } 
          }
          return num;
      }
  }
  </code></pre>
  
  <p>and here is my output:</p>
  
  <blockquote>
    <p>Insert a integer number:<br>
     Invalid value!<br>
    Insert a integer number:<br>
     Invalid value!<br>
    ...</p>
  </blockquote>