import java.io.*;
import java.util.Scanner;
/***********************************************
 * This class experiments with various exceptions
 * and catches them and prints the error message
 * to continue with the program.
 *
 * @author - Jonathan Spiker
 * @version - V1
 ************************************************/
 // Date: 01-17-07
 // Section: 03
 // Lab: 3
 /**
 /  References and Ackknowledgements: I have recieved
 /  no help on this programming assignment
 /
 /***********************************************/
public class Exceptions
{
	/********************************************
	 * This method contains the try catch blocks to
	 * test various exceptions.
	 *
	 * @param argName command line arguments not used
	 *********************************************/
	public static void main(String args[])
	{
		Scanner myScan;
		FileReader fReader;				//for file input
		BufferedReader inputFile;		//for file input
		String fileName;					//string to hold the name of a file
		String str;							//a string used in various try blocks
		int number;							//an integer used in various try blocks
		int number2;						//an integer used in a try block
		int[] intArray = {2, 4, 6};	//an array used in a try block
		
		myScan = new Scanner(System.in);
		
		// try to read from "junk.txt"
		try
		{
			System.out.print("Enter the name of a file to open: ");
			fileName = myScan.nextLine();
			System.out.println("The file we are attempting to open is named: " + fileName);
			
			fReader = new FileReader(fileName);
			inputFile = new BufferedReader(fReader);
			
		}
		//catches any filenotfound exceptions
		catch (FileNotFoundException fnfe)
		{
			System.out.println("The file we attempted to open was not found");
		}
		
		System.out.println();
		

		// try to parse "text" into an integer value
		try
		{
			System.out.print("Enter a number that we will read as a String " +
				"and then parse as and integer: ");
			str = myScan.nextLine();
			
			System.out.println("The string we are trying to parse to an integer is " + str);
			number = Integer.parseInt(str);
		
		}
		//catches any numberformat exceptions
		catch (NumberFormatException nfe)
		{
			System.out.println("The string attempted to be parsed was not an integer string");
		}
		
		System.out.println();
		
		// try to read past the index of 2
		try
		{
			System.out.print("Enter a number in the array to print to: ");
			number = myScan.nextInt();
			myScan.nextLine();
			
			for(int ii = 0; ii <= number; ii++)
			{
				System.out.println("The number in position " + ii + " is " + intArray[ii]);
			}
			
		}
		//catches any arrayindexoutofbounds exceptions
		catch (ArrayIndexOutOfBoundsException aiobe)
		{
			System.out.println("The array index entered is larger than " +
				"the highest index value in intArray");
		}
		
		System.out.println();
		
		// enter a number greater than 3 to prints its character
		try
		{
			str = "blah";
			System.out.print("Enter an index in the range of \"" + str + "\" to print " +
				"out it's character: ");
			number = myScan.nextInt();
			myScan.nextLine();
			
			System.out.println("The index we want to print in the string is " + number);
			
			char myChar = str.charAt(number);
			System.out.println(myChar);
		}
		//catches any stringindexoutofbounds exceptions
		catch (StringIndexOutOfBoundsException siobe)
		{
			System.out.println("The number entered is beyond the index range of the string");
		}
		
		System.out.println();
		
		// try to divide the number by zero
		try
		{
			number = 10;
			System.out.print("Enter a number to divide " + number + " by: ");
			number2 = myScan.nextInt();
			System.out.println("We are attempting to divide " + number + " by " + number2);
			
			number = number / number2;
			
			
		}
		//catches any arithmetic exceptions
		catch (ArithmeticException ae)
		{
			System.out.println("This arithmetic is not valid");
		}
		
		System.out.println();
		
		//notifies user that the program has ended normally
		System.out.println("The program ended normally");
	}
}