import java.util.*;
public class Exceptions
{
	public static void main(String[] args)
	/*************************************************************
	* This program will handle 5 exceptions: NumberFormatException,
	* ArithmeticException, ArrayIndexOutOfBoundsException,
	* InputMismatchException, and NegativeArraySizeException
	*
	* @author-Mike Ellis
	* @Version- V1 1/17/07
		**************************************************************
		* References and Acknowledgements- I Recieved no outside help with this Program
		**************************************************************/
	// Mike Ellis
	// 2:30 Lab
	// 1/17/07
	// Lab-2
	{
		int int1;
		int int2;
		int int3;
		int[] myArray=new int[2];
		String stringToInteger;
		Scanner keyboard=new Scanner(System.in);
		
		/*This will atempt to convert a string to an integer and catch the exception if
		the string is not compatable*/
		System.out.print("type an integer (anything else will cause a NumberFormatException): ");
		try
		{
			stringToInteger=keyboard.nextLine();
			int1=Integer.parseInt(stringToInteger);
		}
		catch(NumberFormatException e)
		{
			System.out.println("is not an integer: cannot be converted from a string");
		}
		
		/*This will attempt to divide 1 by the number entered and will catch the exception
		if the program tries to divide by 0*/
		System.out.print("Enter an integer that isn't 0 (0 will cause an ArithmeticException): ");		
		try
		{
			int1=keyboard.nextInt();
			int2=1/int1;
		}
		catch(ArithmeticException e)
		{
		System.out.println("cannot divide by 0");
		}
		
		/*This will attempt to store the integer 5 in the array element specified by the
		user. It will catch the exception if the number is out of the array's bounds*/
		System.out.print("enter 0 or 1 (any other positive integer will cause an ArrayIndexOutOfBoundsException): ");
		int1=keyboard.nextInt();
		try
		{
			myArray[int1]=5;
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println(int1 + 
			" is not a 1 or 2 (and is out of the array's index bounds)");
		}
		
		/*This will try to assign the number entered to an int value, and will catch the
		exception if anything else is entered*/
		System.out.print("type an integer (anything else will cause anInputMismatchException): ");
		try
		{
		int1=keyboard.nextInt();
		}
		catch(InputMismatchException e)
		{
			System.out.println("not an integer:cannot assign to an int variable");
		}
		
		keyboard.nextLine();  //consumes the last input
		
		/*This will create an array with a number of elements specified by the user.
		 It will catch the exception if a negative number is entered*/
		System.out.print("type a positive integer (anything negative will cause a NegativeArraySizeException): ");
		try
		{
			int3=keyboard.nextInt();
			int[] myArray2=new int[int3];
		}
		catch(NegativeArraySizeException e)
		{
			System.out.println("cannot create an array with negative element number");
		}
		
		System.out.print("This program has ended normally");
	}
}