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

/********************************************************************** 
 *   This class is designed to throw and catch exceptions
 *
 *
 *   author:  Dat Nguyen
 *   date:		1/18/2007
 *   lab3 
 *   section 2
 *
 **********************************************************************/	
    public class Exceptions_Lab_nguyendq_ExceptionLab
   {
   /********************************************************** 
   * This main method contains 5 try-catch blocks for 
   *	5 different exceptions.
   *
   *   @param args  array of string
   **********************************************************/
       public static void main (String [] args)
      {
         String fileName;
         Scanner fileScanner;
         Scanner keyboard;
         String value;
      
         keyboard = new Scanner (System.in);
        
      		/*** 
      		*  FileNotFoundException:  Signals that an attempt to open 
       		*  the file denoted by a specified pathname has failed.
      		**/
         try	
         {  
            System.out.println ("Enter the name of the file: ");
            fileName = keyboard.nextLine();
            System.out.println ("The name of the file is: " + fileName);
            fileScanner = new Scanner (new File (fileName));
         	
            while (fileScanner.hasNext()) 
            {
               value = fileScanner.next();
               System.out.println ("Data in the file is: " + value);
            }
         }  
             catch (FileNotFoundException fnfe)
            {
               System.out.println ("Invalid!  The file you wanted to open was not found");
               System.out.println("The exception message is: " + fnfe.getMessage());
               System.out.println();
            }
         
      		/*** 
      		*  ArithmeticException:  Thrown when an exceptional arithmetic 
       		*  condition has occurred (divide by zero).
      		**/ 
         int numerator, denominator;
         numerator = 12;
         denominator = 0;
      	  
         try
         {
            System.out.println (numerator/denominator);
         }
             catch (ArithmeticException ae)
            {
               System.out.println ("Invalid!  Can not divide by 0.");
               System.out.println("The exception message is: " + ae.getMessage());
               System.out.println();
            }
      	
      		/*** 
      		*  NumberFormatException: Thrown to indicate that the application 
      		*   has attempted to convert a string to one of the numeric types, 
      		*   but that the string does not have the appropriate format.
      		**/ 
         String s;
         String int1 = "CS";
         int intCheck;
         s = "";
      	
         try
         {
            s = int1;
            intCheck = Integer.parseInt(s);
         }
             catch(NumberFormatException nfe)
            {
               System.out.println("Invalid!  Sorry, \""+s+"\" is not an integer.");
               System.out.println("The exception message is: " + nfe.getMessage());
               System.out.println();
            }
      	
      		/*** 
      		*  ArrayIndexOutOfBoundsException: Thrown to indicate that an 
      		*   array has been accessed with an illegal index. The index is either 
      		*   negative or greater than or equal to the size of the array.
      		**/
         int[] arrayCheck = {5, 9, 12};
      			
         try
         {
            System.out.println(arrayCheck[4]);
         }
         
             catch (ArrayIndexOutOfBoundsException aioobe)
            {
               System.out.println("Invalid!  Array index is out of bound. ");
               System.out.println("The exception message is: " + aioobe.getMessage());
               System.out.println();
            }
      		      
      		/*** 
      		*  InputMismatchException:Thrown by a Scanner to indicate that the 
       		*   token retrieved does not match the pattern for the expected type, 
      		*   or that the token is out of range for the expected type.
      		**/
         int numCheck;
      	
         try
         {
            System.out.print("Enter an integer: ");
            numCheck = keyboard.nextInt();
            System.out.println("The interger input is: " + numCheck);
         }
             catch(InputMismatchException ime)
            {
            
               System.out.println("Invalid!  The input is not an integer");
               System.out.println("The exception message is: " + ime.getMessage());
               System.out.print("\nThe program has ended successfully.\n");
            }
      
      } // end main
   } // end class