So I created this small program for class, working with exception events. I am having a problem with the InputMismatchException event. If the user enters an invalid integer like "two" instead of "2", it displays the proper message, but it still adds that to my array. How can I completely disregard the invalid input the user made and not have it added to my array?

Thanks for any help.

Java Code:
import java.util.Scanner;
import java.lang.*;
import java.util.InputMismatchException;

//start of class Lab09b
public class Lab09b {

	public static void main(String[] args) {
		
		// declared variables and arrays
		int roots[] = new int[4];
		int squares[] = new int[4];
		int x = 1;
		int count = 0;
		
		// Scanner input object		
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("This is my robust program!");
		
		// start of while loop, exit loop if 0 is entered
		while (x != 0)
		{
			//User input and exception event catcher			
			try
			{
				System.out.println("Enter a Positive Integer number (0 to quit).");
				x = scanner.nextInt();
			}
			catch (InputMismatchException e)
			{
				System.out.println("Invalid input. Must be an integer within legal range.  Ignoring input, try again...");
				scanner.nextLine();
			}
			
			// if x equals 0, the while loop will exit
			if (x == 0)
			{
				System.out.println("\nLoop is exiting...\n");
				break; //break out of loop
			}
			else 
			{
				//assign input from user to arrays, another exception event
				try
				{
					roots[count] = x;
					squares[count] = x;
					count++; //increment count variable
				}
				catch (ArrayIndexOutOfBoundsException e)
				{
					System.out.println("\nGet Exception: " + e + "\nThe arrays are full. Printing out there contents...\n");
					break;
				}
			}
		}//end of while loop
		
		//print and calculate square root of roots array
		System.out.println("Array of sqaure roots...");
		System.out.println(Math.sqrt(roots[0]) + "   " + Math.sqrt(roots[1]) + "   " + Math.sqrt(roots[2]) + "   " + Math.sqrt(roots[3]));
	
		//print and calculate square of squares array
		System.out.println("Array of squares...");
		System.out.println(roots[0] * roots[0] + "   " + roots[1] * roots[1] + "   " + roots[2] * roots[2] + "   " + roots[3] * roots[3]);		
	}//end of main method
}//end of class Lab09b