import java.io.*;				// for file I/O classes
import java.util.Scanner;	// for scanner class

/************************************************
 * Exceptions provides examples of how to handle 
 * various exceptions
 * 
 * @author - Joshua M. Bryant
 * @version - V1 - Jan 17, 2007
 ************************************************/
 // Lab Time : Wednesday, 1:25 - Section 1
 // Lab 3 : Handling Exceptions
 
public class Exceptions_Lab_bryan5jm_Exceptions
{
	/**********************************************
	 * main method that handles 5 different exceptions.
	 * the exceptions are : 
	 * 	FileNotFoundException fnfe
	 *		NumberFormatException nfe
	 *		StringIndexOutOfBoundsException sioobe
	 *		ArrayIndexOutOfBoundsException aioobe
	 * 	NullPointerException npe
	 *
	 * @param args not used
	 **********************************************/
	public static void main(String[] args)
	{
		int int1,
			charNum;
		int[] intArray;
		char character;
		String fileName,
			numberStr,
			nullStr;				// used to create a NullPointerException
		FileReader freader;
		Scanner keyboard;
		
		keyboard = new Scanner(System.in);
		
		// attempts to open a file provided by the user, and handles the
		// 	exception if a file cannot be found
		System.out.print("Enter the name of the file that you would " +
			"like to open: ");
		fileName = keyboard.nextLine();
		System.out.println();
		try
		{
			freader = new FileReader(fileName);
			System.out.println(fileName + " was found.");
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("Error: The file " + fileName + 
				" was not found.\n");
		}
		
		// attempts to convert a string provided by the user into an integer,
		//		and handles the exception if the string holds non-integer characters
		System.out.print("Enter a string that only holds integers: ");
		numberStr = keyboard.nextLine();
		System.out.println();
		try
		{
			int1 = Integer.parseInt(numberStr);
			System.out.println(int1 + " was stored in number.");
		}
		catch (NumberFormatException nfe)
		{
			System.out.println("Error: The string you entered has a character " +
				"that is not an integer.\n");
		}
		
		// attempts to find a position of numberStr provided by the user, and handles
		// 	the exception if the position is out of bounds
		System.out.print("Enter the position of the character in numberStr " +
			"that you wish to find: ");
		charNum = keyboard.nextInt();
		System.out.println();
		try
		{
			character = numberStr.charAt(charNum);
			System.out.println(character + " was stored in character.");
		}
		catch (StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("Error: The index was out of the bounds of the string.");
		}
		
		// sets an array, and attempts to assign values provided by the user
		// the loop assigns an array value out of bounds, and the block handles the exception
		intArray = new int[3];
		try
		{
			for(int index = 0; index <= 3; index++)
			{
				System.out.print("Enter the values for the integer array: ");
				intArray[index] = keyboard.nextInt();
				System.out.println();
				System.out.println("inArray[" + index + "] assigned " + intArray[index]);
			}
		}
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("\nError: The array index has exceeded the " +
				"bounds of the array.\n");
		}
		
		// attempts to assign nullStr (with a null value) to numberStr
		// handles the NullPointerException npe
		nullStr = null;
		try
		{
			System.out.println("Program assigning numberStr to nullStr.");
			numberStr.contentEquals(nullStr);
		}
		catch (NullPointerException npe)
		{
			System.out.println("Error: numberStr was assigned a String " +
				"containing a null value.");
		}
		
		System.out.println("This program ended normally.");
	}
}