/**********************************
 * Jeremy Blanchette
 * Professor Adams
 * Section 1 (MW 1:25-2:15)
 * Lab 3
 * 1-17-07
 **********************************/

import java.util.Scanner;

/********************************************
 * Will ask for user imput
 * to handle 5 different kinds of exceptions
 ********************************************/
public class Exceptions_Lab_blanchje_Exceptions
{
	/*****************************
	 * Will ask for user input and
	 * run exception handlers
	 * params: none
	 *****************************/
	 public static void main(String[] args)
	 {
	 	Scanner keyboard;
		String letters;//used for inputted letters
		int number; //used for user inputted numbers
		int number2; //same as above
		int[] array = {0, 1, 2, 3};
			
		keyboard = new Scanner(System.in);
		
	 	System.out.println("Welcome to the exception handler!  Hope you have fun.\n");
		System.out.println("Enter a number (enter non-numeric characters for exception):");
		try
		{
			number = keyboard.nextInt();
			System.out.println("You entered " + number);
		}
		//InputMismatchException for letters instead of numbers
		catch(java.util.InputMismatchException ime)
		{
			//catches if they put in non-numerical characters
			System.out.println("You have to enter a number.\n");
		}
		System.out.println("Now there is an array with 4 values.  How many values would you like to print? (Exception will occur past 3):");
		keyboard.nextLine();
		number = keyboard.nextInt();
		try
		{
			System.out.println("You entered " + number);
			for (int i = 0; i <= number; i++)
			{
				System.out.println(array[i]);
			}
		}
		//ArrayIndexOutOfBoundsException
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			// catches if the index goes out of the bounds of the array
			System.out.println("The array doesn't go that far\n");
		}
		System.out.println("Please enter a number (Enter a large number, I mean it, reallllyy large, for a really cool exception handler): ");
		try
		{
			number = keyboard.nextInt();
			
			System.out.println("You entered " + number);	
		}
		//InputMismatchException for too large of a number
		catch(java.util.InputMismatchException ime)
		{
			// catches if the number is too large for an integer value
			System.out.println("The number is too large to put in an integer!\n");
		}
			System.out.println("Enter a number to parse into an integer: ");
			keyboard.nextLine();
			letters = keyboard.nextLine();
			System.out.println("You entered " + letters);
		try
		{
			number = Integer.parseInt(letters);
		}
		//NumberFormat Exception
		catch(NumberFormatException nfe)
		{
			// catches if the integer attempts to parse a string
			System.out.println("You have to enter numbers\n");
		}
		System.out.println("Now pick a number to divide 25 by (enter 0 for an exception) :");
		number = keyboard.nextInt();
		System.out.println("You entered " + number);
		try
		{
			number2 = 25 / number;
			System.out.println("25 / " + number + " = " + (25 / number));
		}
		catch(ArithmeticException ae)
		{
			System.out.println("You can't divide by zero\n");
		}
		System.out.println("\nThis program terminated normally.");
	}	
}
