title: How to handle invalid input when using Scanner.nextInt()
body: <p>I am a beginner and i wrote a java program that allows you to enter n numbers and it displays the max, min and average only if the number -5 is entered, my program its not displaying correctly and i need some help. I want to use try/catch to catch errors when a string is entered instead integer.</p>
  
  <pre><code>        import java.util.Scanner;
  public class Average{
  public static void main(String[]args){
  
  System.out.print("Enter any integer numbers or -5 to quit:");
          Scanner scan =new Scanner(System.in);
      double avg = 0.0;
      int number = -1;
      double avg = 0.0;
      double sum = 0;
      int count = 0; 
      int min = Integer.MAX_VALUE;
      int max = Integer.MIN_VALUE;
  
      try {
      while((scan.nextInt())!= -5)
         {
       if (count != 0) {
          avg = ((double) sum) / count;
      count++;
      }   
           if (number &gt; max){
          max = number;
          }
          if(number &lt; min){
         min = number;
          }
          catch (InputMismatchException e) {
        System.out.println("please enter only integer numbers");
  
      System.out.println("Average : " + avg); 
      System.out.println("maximum : " + max);
      System.out.println("minimum : " + min);
  }
  }
  }
  }
  }
  </code></pre>