
import java.util.*;
/***********************************************
 * This class tests different types of exception
 *
 * @author - Lauren Jones
 * @version - V1 - 1.17.2006
 *
 * Secion: 3
 ************************************************/
 /*
 /  References and Ackknowledgements: I have recieved
 /  no help on this programming assignment
 /
 /***********************************************/
public class Exceptions
{
	/********************************************
	 * this method tries and catches all the following exceptions:
	 * --ArrayIndexOutOfBoundsException
	 * --NullPointerException
	 * --InputMismatchException
	 * --StringOutOfBoundsException
	 * --ArithmeticException
	 *
	 * @param argName argDescription
	 *********************************************/
	public static void main(String args[])
	{
		int index;
		int [] array;
		double number;
		Scanner keyboard;
		int charNum;
		String string;
		String word;
		int num1, num2;
		double result;
		
		keyboard = new Scanner(System.in);
		number = 0;
		charNum = 0;
		num1 = 0;
		num2 = 0;
		
		//Array Index Out of Bounds Exception
		try
		{
			System.out.print("Enter in size of array: ");
				index = keyboard.nextInt();
			
			array = new int[index];
		
			for(int x = 0; x <= index; x++)//there is no array element at array size, it stops at index - 1 and throws an exception
				System.out.println("Array element " + x + " of size " + index + " is: " + array[x]);
		}
		
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("Array index is out of bounds.");
			
		}
		
		//Null Pointer Exception
		try
		{
			keyboard.nextLine();
			
			System.out.print("\nEnter in a word: ");
			string = keyboard.nextLine();
			
			if(string.equals("null")) //if "null" is entered, the length cannot be determined and throw an exception
				string = null;
			
			System.out.print("The length of the String is: " + string.length());
		
		}
		
		catch(NullPointerException npe)
		{
			System.out.println("NullPointerException: no length, String is null.");
		
		}
		
		//Input Mismatch Exception
		try
		{
			System.out.print("\n\nEnter in number: ");
				number = keyboard.nextDouble();
			System.out.println("You have entered " + number); //if you enter something other than a number, it was throw an exception
			
		}
		
		catch(InputMismatchException ime)
		{
			System.out.println("InputMismatchException: This is not a number.");
		
		}
		
		//String Out Of Bounds Exception
		try
		{
			word = "laurenjones";
			
			keyboard.nextLine();
			
			System.out.print("\nEnter in a number to locate character: ");
				charNum = keyboard.nextInt();
			
			//if you enter a character greater than the amount of characters, it will throw an StringOutOfBoundsException
			System.out.println("Here is the character at position " + charNum + " : " + word.charAt(charNum)); 
		}
		
		catch(StringIndexOutOfBoundsException siobe)
		{
			System.out.println("There is no character at position " + charNum + ".");
		
		}
		
		//Arithmetic Exception
		try
		{
			System.out.print("\nEnter in first number for division: ");
				num1 = keyboard.nextInt();
			System.out.print("Enter in second number for division: ");
				num2 = keyboard.nextInt();
			
			result = num1 / num2; //if the second number is zero, it will throw an ArithmeticException
			
			System.out.print(num1 + " / " + num2 + " = " + result);
		}
		
		catch(ArithmeticException ae)
		{
			System.out.println("Arithmetic Exception in " + num1 + " / " + num2 + ".");
		}
		
		System.out.println("This program has ended normally.");
	}
} 