   import java.util.Scanner;
   import java.io.*;
/***********************************************
 * This class creates exceptions and the exception
 * handler will handle them
 *
 * @author - Matt Jenkins
 * @version - V1 - Jan. 17, 2007
 ************************************************/
 /*
 /  References and Ackknowledgements: I have recieved
 /  no help on this programming assignment
 /
 /***********************************************/
 /*
 /Matt Jenkins
 /lab 3
 /Jan 17, 2007
 /section 2
 */
    public class Exceptions_Lab_jenkinmj_ExceptionHandler
   {
   /********************************************
   * Creates exceptions then handles them
   *
   * 
   *********************************************/
       public static void main(String args[])
      {
      
      	//used for the first exception, creates an array with 4 instances
         int[] array;
         array = new int[4];
			//used in second exception, store user input values
         int number;
         int divisor;
			//used in third exception, creates three 3 string objects
			Object x[] = new String[3];
			//used in fourth exception, used for file reading
         String fileName;
         int value;
         Scanner fileScanner;
         
      
         Scanner keyboard;
         keyboard = new Scanner(System.in);
      
      	
         try
         {
            for (int i = 0; i <= 4; i++)//causes an exception because it counts to 5 instead of 4
            {
            
               array[i] = i;
               System.out.print(array[i] + "\n");
            
            }
         
         }
             catch (ArrayIndexOutOfBoundsException aioobe)//catches the exception
            {
               System.out.print("The array is only ");
               System.out.print(aioobe.getMessage());//displays what is wrong with the array
               System.out.print(" units big.\n");
            }
      
      	
         System.out.print("Enter a number: ");
         number = keyboard.nextInt();
      
         System.out.println();
			
   		//if 0 is entered it will cause an exception   
         System.out.print("Enter a number to divide that number by: ");
         divisor = keyboard.nextInt();
      
         System.out.println();
      
         try//if the second number is 0 then it will print an appropriate message
         {
            System.out.print("The result of " + number + " divided by " + divisor + " = " + number / divisor);
         }
             catch (ArithmeticException ae)
            {
            
               System.out.print("You can't divide by 0.\n");//exception message
            }
      
      
         try
         {
            x[0] = new Integer(0);//trys to make one instance of the string obkect array an int
         }
             catch (ArrayStoreException ase)//catches the exception and handles it
            {
               System.out.print("\nYou can't store an integer in a string object array\n");
            
            }
      
      
      
      	//asks for filename the user wants to access
         System.out.println (" \nplease enter name of file holding integers and hit return ");
         fileName = keyboard.next();
      
         System.out.println ("\nThe name of the file you want to open is " + fileName);
         
         try
         { 
            fileScanner = new Scanner (new File (fileName));
            while (fileScanner.hasNextInt()) 
            {
               value = fileScanner.nextInt();//if file is found it prints the integers
               System.out.println ("\nThe value you got from the file is " + value);
            }
         }  
             catch (FileNotFoundException fnfe)//if file not found then it prints an exception message
            {
               System.out.println ("The file you wanted to open was not found\n\n");
            
            }
      
      
         try //trys to take the length of a variable with a null value
         {
            String string = null;
            int length;
         	
            length = string.length();
            System.out.print(length);
         }
             catch (NullPointerException npe)//catches the exception and displays exception message
            {
               System.out.print("You can't take the length of a variable with a value of null");
            
            }
      
		System.out.print("\n\nThe program has ended normally.");//tells users that the program ended without exceptions
      	
      
      }
   }  
  
