How do I check whether a scanned integer is indeed an integer? : <p>I'm pretty new to Java, I program in C# which is very similar, but I have some troubles defending my code in java.</p>

<p>I'd like to defend my code against illegal input such as string,char,special char.</p>

<p>I want every time an input other than int to STAY in the do-while loop(exact place marked with ?????????)</p>

<p>Here is my code, pretty simple, it takes numbers and calculates the minimum number,maximum, avg,sum.
If the number -1 is entered, either the program won't start or the loop would stop and calculation will be printed.</p>

<p>Thanks in advance for your help!</p>

<pre><code>public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    //Variables
    int count=0;
    int sum=0;
    int max=0;
    int min=0;

    //Initializing minimum value for a reference
    System.out.print("Enter a number ");
    int number = scan.nextInt();
    if(number!=-1){
        min=number;
        count++;
        sum+=number;
    }

            while(number!=-1){   

        do {
            System.out.print("Enter a number or -1 to stop: ");
            number = scan.nextInt();

        }while((Integer.toString(number)==null)||???????????? (Check for illegal input));

        if(number==-1){
            break;
        }
        else {  
            sum+=number;
            count++;
            if(number&gt;max) {
                max=number;
            }
            else if(number&lt;min) {
                min=number;
            }
        }
    }

    if(count!=0)
    {
        System.out.println("The Maximum number is: "+max);
        System.out.println("The Minimum number is: "+min);
        System.out.println("The Sum of all entered numbers is: "+sum);
        System.out.println("The Average of all entered numbers is: "+(double)sum/(double)count);
        System.out.println("T H E    E N D");
    }


    else {
        System.out.println("T H E    E N D");
    }


    scan.close();

}
</code></pre>

<p>}</p>
