title: Why does my java program keep printing the exception in try catch loop?
body: <p>I can't seem to find the problem. The output is correct, until the exception is caught. When I catch it, the program prints: </p>
  
  <blockquote>
    <p>Please Give An Integer: Not Valid Integer, Please Try Again Please<br>
    Give An Integer: Not Valid Integer, Please Try Again...</p>
  </blockquote>
  
  <p>infinitely. I know it must be a stupid mistake.</p>
  
  <pre><code>import java.util.InputMismatchException;
  import java.util.Scanner;
  
  public class Lab4Exercise1 {
  
      public static void main(String[] args){ 
  
          Scanner sc = new Scanner(System.in);
          boolean caught;
          int number=-1;
          int counter = 0;
          int[] array = new int[5];
  
          do {
              caught = false;
              System.out.print("Please Give An Integer: ");
  
              try {
                  number = sc.nextInt();
              }catch (InputMismatchException e) {
                  System.out.println("Not Valid Integer, Please Try Again");
                  caught = true;
              }
  
              if (caught == false) {
                  counter += 1;
                  array[counter] = number;
              }
  
          }while(counter &lt; 5);
  
          System.out.println("Your input was:");
          for(int i = 0; i &lt;= 4; i++) {
              System.out.println(array[i]);
          }
          sc.close();
      }
  
  }
  </code></pre>