import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


/**
 * Exceptions.java - This class demonstrates five different
 * types of exceptions
 * 1. FileNotFoundException
 * 2. ArrayIndexOutOfBoundsException
 * 3. NullPointerException
 * 4. IllegalArgumentException
 * 5. NegativeArraySizeException
 * 
 * 
 * @author Andrew Herrington
 * @date 1/17/06
 * @lab Lab3
 * @section 1
 */

public class ExceptionsJones
{
	/** ========================================================
	* 
	*  main method - runs 5 blocks of codes to generate and handle
	*  exceptions
	*              
	*  @param args   command line arguments not used in this application
	*/
	public static void main(String[] args) {
		
		//generic scanner for the whole method
		Scanner keyboard;
		keyboard = new Scanner (System.in);
		
		//variables for filenotfoundexception block.
		Scanner fileScanner; 
		

		//variables for array out of bounds exception block
		int[] array = {1,1,2,3,5,8,13,21};
		int whichPlace;
		
		//variables for NullPointerException block
		StringTokenizer tokenizer;
		tokenizer = null;
		boolean npeDone;

		//variables for IllegalArguementException
		String userInput;
		
		//variables for NegativeArraySizeException
		int[] naseArray;
		
		//This block of code generates a file not found
		//exception if the user inputted file name is not
		//found.
		
		try {
			//ask the user for a filename input.
			System.out.println("Please input a filename folowed by" +
				" that file's extension.");
	
			//instansiate fileScanner with the users input
			fileScanner = new Scanner(new File(keyboard.next()));
			
			//print the first integer in the file if it exits.
			System.out.println("The first integer in the file is: " 
					+fileScanner.nextInt());
			
		} catch (FileNotFoundException fnfe) {

			System.out.println("The file name you entered was not found.");

			System.out.println(fnfe.getMessage());
		}
		
		//end FileNotFoundException block
		
		
		//This block of code generates an ArrayIndexOutOfBounds
		//exception and handles it.
		
		try{
		
		//asks user for input.  To get the exception, user has to input a number
		//greater then 8
	
		System.out.println("\n\nI can print Fibonacci up to 8 places." +
				" Which one would you like me to print? " +
				"(Enter an int up to 8)");
	
		whichPlace = keyboard.nextInt();
		
		System.out.println(array[whichPlace]);
		
		}catch(ArrayIndexOutOfBoundsException aioobe){
			
			System.out.println("You entered an array index that is " +
					"larger then my array. ");
			
			System.out.println(aioobe.getMessage());
		}
		
		//end ArrayIndexOutOfBounds block
		
		
		//This block of code generates a NullPointerException
		//and handles it
			try{
				//this try deliberately causes an exception every time.
				System.out.println("\n\nPrinting the first token in " +
						"Tokenizer.");
				System.out.println(tokenizer.nextElement());
			
			
			}catch(NullPointerException npe){
			
				System.out.println("You've generated a " +
						"NullPointerException.");

				System.out.println(npe.getMessage());

			}//end catch

		
			//This block of code generates an IllegalArgumentException
			//exception and handles it.
			
			try{
				//getting user to input an int.  To generate the exception
				//user must enter a non int.	
				System.out.println("\n\nPlease input an integer to reprint!");
				
				System.out.println(Integer.parseInt(keyboard.next()));
				
				
			}catch(IllegalArgumentException iae){
				
				System.out.println("The integer class cannot convert your" +
						" input.");
				
				System.out.println(iae.getMessage());
			}
			
	    	//end IllegalArguementException block
		
			
			//This block of code generates an NegativeArraySizeException
			//exception and handles it.
			
			try{
				//Asks the user for an int.  To generate the error, user mus
				//enter a negative integer.
				System.out.println("\n\nPlease input an integer to make " +
						"an array.");
				userInput = keyboard.next();
				
				naseArray = new int[Integer.parseInt(userInput)];
				
			}catch(NegativeArraySizeException nase){
				
				System.out.println("You inputted a negative integer!" +
						"  You cannot do that!");
				
				System.out.println(nase.getMessage());
				
			}
			
			//end NegativeArraySizeException block

	} //end main

}
