/***********************************************
 * ExceptionLab will handle five possible
 * exceptions for learning purposes.
 * It handles:
 * 1) FileNotfoundException
 * 2) ArithmeticException
 * 3) ArrayIndexOutOfBoundsException
 * 4) IllegalArgumentException
 * 5) StringIndexOutOfBoundsException
 * 
 * @author Kristopher Kalish
 * @version 1 - January 17th, 2007
 * 
 * Lab 3
 * Section 1 
 * 
 ************************************************/
 /*
  *  References and Acknowledgements: I have recieved
  *  no unauthorized help on this assignment.
  *
  */

import java.util.Scanner;
import java.io.*;

public class Exceptions_Lab_kalishkl_ExceptionLab
{
	/**
	 * main() will generate the five potential exceptions
	 * and prompt the user.
	 */
	public static void main(String args[])
	{
		Scanner fileScanner;	// reading from file
		Scanner keyboard;		// gets keyboard input
		
		File	file;			// file to open
		
		String	integer;		// String version of an int
		String  nothing;		// a meaningless String
		
		// instantiate our keyboard scanner
		keyboard = new Scanner(System.in);
		
		// This try block will throw a FileNotFoundException
		// if it tries to open a file that doesn't exist
		try
		{
			// prompt the user
			System.out.print("Enter a filename to open: ");
			file = new File(keyboard.next());
			
			// attempt to open the file
			System.out.println("Opening " + file.getName() + "...");
			fileScanner = new Scanner(file);
			fileScanner.close();
		} 
		catch(FileNotFoundException fnfe)
		{
			System.out.println("Sorry the file you entered " 
					+ " could not be found...");
		}
		
		// The second try block will throw an ArithmeticException
		// by attempting to divide by zero
		try
		{
			int x;   // arbitrary integer values
			int y;
			int z;
			
			x = 200; // give them some values
			y = 0;
			
			// divide by zero
			z = x / y;
		}
		catch(ArithmeticException ae)
		{
			// alert the user of what has happened
			System.out.println("Attempted to divide by zero!");
		}
		
		// this block will never operate properly
		// it throws an ArrayIndexOutOfBoundsException
		try
		{
			// lets make an array of five elements
			int array[] = { 1, 2, 3, 4, 5 };
			
			// Print the sixth element
			System.out.println(array[5]);
		}
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			// oops!
			System.out.println("Attempted to access an array "
					+ " element out of bounds!");
		}
		
		// This next block will throw an IllegalArgumentException
		// by asking for input and attempting to parse to Integer
		try
		{
			// prompt the user input
			System.out.print("Input an integer: ");
			integer = keyboard.next();
			
			// attempt to part into an ingeger
			System.out.println("Parsing '" + integer + "'");
			Integer.parseInt(integer);
		}
		catch(IllegalArgumentException iae)
		{
			// warn the user and keep going
			System.out.println("You did not input an integer!");
		}
		
		// Time for a StringIndexOutOfBoundsException
		// this will be done by accessing individual 
		// characters in a String
		try
		{
			// Prompt for an arbitrary String
			System.out.print("Give me a String: ");
			nothing = keyboard.next(); //store it
			
			// echo and attempt to access element 20 in 'nothing'
			System.out.println("Character 21 in " + "'" + nothing +
					"' is" + nothing.charAt(20));
			
		}
		catch(StringIndexOutOfBoundsException sioobe)
		{
			// warn the user
			System.out.println("Tried to access a string " + 
					"that was not in bounds!");
		}
		
	}
}
