   import java.util.*;
   import java.io.*;

/**********************************************************************
* This class causes the exception objects FileNotFoundException,
* ArrayIndexOutOfBoundsException, IllegalArgumentException,
* NumberFormatException, and Arithmetic Exception.
*
* @author Jonathan Herman
* @version 1
**********************************************************************/
// Elizabeth Adams
// Date: 1/17/07
// Lab: 3
// Section: 2

    public class Exceptions_Lab_hermanjl_ExceptionPlay
   {
   /**
   * This method causes many exceptions.
   *
   * @exception aioobe ArrayIndexOutOfBoundsException
   * @exception iae IllegalArgumentException
   * @exception nfe 
   * @exception fnfe FileNotFoundException
   * @exception ae ArithmeticException
   * @param args command line arguments-unused
   */
       public static void main (String [] args)
      {
         int holder; // to hold input
         String inputFile; // to hold the name of the input file
         Scanner input, // to read input from a file
              keyboard; // to read from keyboard
         int [] testArray;
      	
         testArray = new int[2];
         keyboard = new Scanner (System.in); // initializes keyboard
      
      // attempts to print out data from an array beyond the last int
         try
         {
         // causes an exception by trying to read beyond the last int
            System.out.println (testArray[2]);
         }
             catch (ArrayIndexOutOfBoundsException aioobe)
            {
            // tells the user what has happened
               System.out.println ("The index this program attempted to read does not exist.");
            }
      
      // attempts to parse an int from a string
         try
         {			
            Integer.parseInt("cow"); // "cow" is not a legal argument, causes exception
         }
             catch (IllegalArgumentException iae)
            {
            // tells the user what has happed
               System.out.println ("The program attempted to run a method while supplying an illegal argument.");
            }
      
         try
         {
            Integer.parseInt ("cow"); // cannot find an int in "cow," causes exception
         }		
             catch (NumberFormatException nfe)
            {
               System.out.println ("The program can't parse an int from the word cow.");
            }
      
      // gets the name of a file to try and read from and assigns to inputFile
         System.out.print ("What is the name of a file we will try to find: ");
         inputFile = keyboard.nextLine();
      	
      // echoes the file name
         System.out.println("The file we will attempt to find is " + inputFile);
      
      // attempts to create a file object
         try
         {		
         // assigns a file that does not exist to input, causing an exception
            input = new Scanner (new File (inputFile));
         }		
             catch (FileNotFoundException fnfe)
            {
            // tells the user what has happened
               System.out.println ("The file " + inputFile + " cannot be found.");
            }
      
      // attempts to print out the result of a division operation
         try
         {
            System.out.println (4 / 0); // illegal division causes exception
         }
             catch (ArithmeticException ae)
            {
            // tells user what has happened
               System.out.println ("The program attempted to divide by 0.");
            }
      	
         System.out.println ("This file has ended successfully.");
      }
   }