title: Checking if an input is an Integer using exceptions - Java
body: <p>My method must request input from the user, check if it is an integer, and if it is return that integer. I attempted this using a try catch and the InputMismatchException.</p>
  
  <p>I am running into an issue when it loops, if I input a non integer, it continuously spits out "Invalid input" "Enter an integer: " instead of actually asking for one.</p>
  
  <pre><code>public int getInteger(){
      Scanner i = new Scanner(System.in);
      int value = 0;
  
      for(boolean test = false; test == false;){
          try{
          System.out.println("Enter an integer: ");
          value = i.nextInt();
  
          test = true;
          return value;
          }
          catch(InputMismatchException e){System.out.println("Invalid input");}
      }
      return value;
  } 
  </code></pre>