title: How to use Try/Catch to prevent String input where an Int is needed
body: <pre><code>/*
  * To change this license header, choose License Headers in Project Properties.
  * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */
  package freetime;
  
  import java.util.InputMismatchException;
  import java.util.Scanner;
  
  /**
  *
  * @author Andy
  */
  public class NewClass {
  public static void main(String[] args){
      Scanner scan=new Scanner(System.in);
      int userInput=0,notInt=0,newInput=0;
      System.out.println("Enter a integer number");
      while (notInt == 0){
          try {
              newInput=scan.nextInt();
              userInput=newInput;
              if (userInput != newInput){
                  notInt=0;
              }
              if (userInput == newInput){
                  notInt=1;
              }
          }
          catch(InputMismatchException e){
              System.out.println("That is not an integer, please try again." );
          }
      }
      System.out.println(userInput);
  
   }
  }
  </code></pre>
  
  <p>I am trying to prevent a string input and allow the user to re input as an int. i cannot seem to get it to work properly, i also did this without the while loop. I think my understanding of how the Try Mismatch functions work is causing some issues. </p>
  
  <p>Thanks for the help.</p>
  
  <pre><code>Scanner scan=new Scanner(System.in);
      int userInput=0,notInt=0,newInput=0;
      boolean runL=true;
      while (runL){
          System.out.println("Enter a integer number");
          if (scan.hasNextInt()){
             userInput=scan.nextInt();
             runL=false;
          }
          else {
              System.out.println("That is not an integer, please try again.");
          }
      }
      System.out.println(userInput);
  </code></pre>
  
  <p>So i changed the code to this but I am still running into the loop continuing to go after something other than an int is entered. What am i doing wrong?</p>
  
  <pre><code>    int userInput=0,notInt=0,newInput=0;
      boolean runL=true;
      while (runL){
          Scanner scan=new Scanner(System.in);
          System.out.println("Enter a integer number");
          if (scan.hasNextInt()){
             userInput=scan.nextInt();
             runL=false;
          }
          else {
              System.out.println("That is not an integer, please try again.");
          }
      }
      System.out.println(userInput);
  </code></pre>
  
  <p>I've found the problem, I needed to create the scanner object inside of the While loop. Thanks for the help!</p>