title: How to loop user input until an integer is inputted?
body: <p>I'm new to Java and I wanted to keep on asking for user input until the user enters an integer, so that there's no InputMismatchException. I've tried this code, but I still get the exception when I enter a non-integer value. </p>
  
  <pre><code>int getInt(String prompt){
          System.out.print(prompt);
          Scanner sc = new Scanner(System.in);
          while(!sc.hasNextInt()){
              System.out.println("Enter a whole number.");
              sc.nextInt();
          }
          return sc.nextInt();
  }
  </code></pre>
  
  <p>Thanks for your time!</p>