import java.util.*;
import java.io.*;
/***********************************************
 * This class will test 5 different exceptions: InputMismatchException,
 * StringIndexOutOfBoundsException, ArrayIndexOutOfBoundsException,
 * NumberFormatException, and FileNotFoundException.
 *
 * @author - Tim Ward
 * @version - V1 - 1/17/07
 ************************************************/
 /*
 /  References and Ackknowledgements: I have recieved
 /  no help on this programming assignment
 /
 /***********************************************/
 // Tim Ward
 // Section 2
 // 1/17/07
 // Lab 3
 
public class Exceptions
{
	/********************************************
	 * This method will test 5 different exceptions: InputMismatchException,
 	 * StringIndexOutOfBoundsException, ArrayIndexOutOfBoundsException,
 	 * NumberFormatException, and FileNotFoundException.
	 *********************************************/
	public static void main(String args[])
	{
		//variable declarations
		Scanner keyboard;
		Scanner fileReader;
		int stringInt, parseValue, fileValue;
		String shortString, str, fileName;
		int[] numbers = { 0, 1, 2 };
		
		keyboard = new Scanner(System.in);
		
		/********************************************
		* InputMismatchException
	 	*********************************************/
		try
		{
			System.out.print("Enter an integer: ");
			stringInt = keyboard.nextInt();
			keyboard.nextLine();
			System.out.println();
		}
		/********************************************
		* @exception ime handles an InputMismatchException
	 	*********************************************/
		catch (InputMismatchException ime)
		{
			System.out.println("You did not enter an integer.");
		}
		
		/********************************************
		* StringIndexOutOfBoundsException
	 	*********************************************/
		try
		{
			//input a string
			System.out.print("Enter a string: ");
			shortString = keyboard.nextLine();
			
			//echo the input
			System.out.println("You entered the string " + shortString);
			
			//output the 6th character in the string
			System.out.println("The 6th character is " + shortString.charAt(5) + "\n");
		}
		/********************************************
		* @exception sioobe handles an StringIndexOutOfBoundsException
	 	*********************************************/
		catch (StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("You did not enter a long enough string.\n");
		}
		
		/********************************************
		* ArrayIndexOutOfBoundsException
	 	*********************************************/
		try
		{
			//proceed through the array of ints and output value
			for (int i = 0; i <= 3; i++)
			{
				System.out.println(numbers[i]);
			}
		}
		/********************************************
		* @exception aioobe handles the ArrayIndexOutOfBoundsException
	 	*********************************************/
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("There are no more numbers in the array.\n");
		}
		
		/********************************************
		* NumberFormatException
	 	*********************************************/
		try
		{
			//input a string to be parsed into an int
			System.out.print("Enter a string of numbers: ");
			str = keyboard.nextLine();
			
			//echo the input
			System.out.println("You entered the string " + str);
			
			//parse the string into an int
			parseValue = Integer.parseInt(str);
			System.out.println(parseValue);
		}
		/********************************************
		* @exception nfe handles a NumberFormatException
	 	*********************************************/
		catch (NumberFormatException nfe)
		{
			System.out.println("You did not enter your string in the correct format.\n");
		}
		
		/********************************************
		* FileNotFoundException
	 	*********************************************/
		try
		{
			//input the file name to be found
			System.out.print("Enter the name of the file to be found: ");
			fileName = keyboard.nextLine();
			
			//echo the input
			System.out.println("The file you are looking for is " + fileName);
			
			fileReader = new Scanner (new File (fileName));
			
			//output the next int found in the file
			fileValue = fileReader.nextInt();
			System.out.println("The value in the file is " + fileValue);
		}
		/********************************************
		* @exception fnfe handles a FileNotFoundException
	 	*********************************************/
		catch (FileNotFoundException fnfe)
		{
			System.out.println("The file you were looking for was not found.\n");
		}
	}
}  
  