/*********************************
* Spencer Sayce
* 1/17/07
* Section 1
**********************************/
import java.util.Scanner;
import java.io.FileReader;
/*********************************
* This class will run five blocks
* of code that will each cause a 
* different exception. The class
* will catch all of these exceptions
*
* ArrayIndexOutOfBoundsException
* InputMismatchException
* NumberFormatException
* FileNotFoundException
* ArithmeticException
**********************************/
public class Exceptions_Lab_saycese_Exceptions
{
	/*********************************
	* This method will run five blocks
	* of code that will each cause a 
	* different exception. The method
	* will catch all of the exceptions
	* mentioned above
	**********************************/
	public static void main(String args[])
	{
		//This code will try to print more elements than the array is holding
		//creates the array
		int[] number = {1, 2, 8, 5};
		try
		{	
			//prints out one more element than the array holds
			for (int i = 0; i <= 4; i++)
				System.out.println(number[i]);
		}
		//catches the exception when it happens
		catch (java.lang.ArrayIndexOutOfBoundsException aioob)
		{
			System.out.println("There are not enough elemts in the array.");
		}
	
		
		//This code will catch an input exception if the user does not use an integer
		//creates the variables
		Scanner kb;
		int input; 
		kb = new Scanner(System.in);
		try
		{
			//promts user for a number
			System.out.println("Type a number.");
			//reads in only a number
			input = kb.nextInt();
		}
		//catches the exception if something other than a number is input
		catch(java.util.InputMismatchException ime)
		{
			System.out.println("You did not type a number.");
		}
		
		
		//This code will try to parse a String and will then catch the exception
		//creates the variables
		String word;
		word = "Hello";
		int parse;
		//parses a String
		try
		{
			parse = Integer.parseInt(word);
		}
		//Catches the number format exception
		catch(java.lang.NumberFormatException nfe)
		{
			System.out.println("Cannot parse a String.");
		}	
		
		
		//This code will will try to locate a file that is not there and catch the exception
		//creates the variables
		FileReader reader;
		//Trys to read a file that is not there
		try
		{
			reader = new FileReader("NoFile.txt");
		}
		//catches the file not found exception
		catch(java.io.FileNotFoundException fne)
		{
			System.out.println("There is no file.");
		}
		
		
		//This code will catch a Arithmetic Exception (divide by zero)
		int num, result;
		kb = new Scanner(System.in);
		try		
		{
			//promts user for a number
			System.out.println("Type a number to divide 25 by.");
			//reads in a number
			num = kb.nextInt();
			//finds and prints the result
			result = 25 / num;
			System.out.println("The result is " + result);
		}
		//if the number was zero it will catch the exception and let the user know
		catch(java.lang.ArithmeticException ari)
		{
			System.out.println("Cannot divide by zero.");
		}
		//prints that the program has ended where it should
		System.out.println("The program has ended normally.");
	}	
}