/****************************************
* @author - Michael Beaumont
* @version - VI - 1/17/07
* Lab - 3
* Section 1
* This program deals with Exceptions.  The exceptions used are ArrayIndexOutOfBoundsException,
* FileNotFoundException,IllegalArgumentException, StringIndexOutOfBoundsException, ArithmeticException
****************************************/
import java.util.Scanner;
import java.io.*;
public class Exceptions_Lab_beaumomw_Exception
{
/*
* This is the main, where all variables are declared and all excpetions are handled
* @param args command line input
*/
	public static void main (String[] args)
	{
		//declare variables
		  int input;
		  int number;
		
		//declare reference types  
		  String value;
		  String file;
		
		//declare scanner objects	  
		  Scanner keyboard;
		  Scanner fileScanner;
		
		//instantiate and initialize  
		  int[] array = {1, 2, 3, 4, 5, 6};  
		  keyboard = new Scanner(System.in);
		  
		//this try block is printing indecies of the array
		try
		{
        for(int index = 0; index <= 6; index++)
		  {
		  		System.out.println(array[index]);
		  }
		}
		
		//Too many indecices were trying to be printed and generated this exception.  Index 7 
		// was trying to be printed, but there are only 6 indecies.
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.print("\n");
			System.out.println("Tried to print too many Indecies");
			System.out.println("This is an array index out of bounds");
		}
		
		
		System.out.println("\n\nEnter the name of the file");
		file = keyboard.nextLine();  //Get the name of the file
	
	//This is trying to open the name of the file the user asked for and is reading back the input from the file
		try
		{
			fileScanner = new Scanner (new File (file));
			while (fileScanner.hasNextInt()) 
			{ 
				input = fileScanner.nextInt(); 
				System.out.println (" The value you got from the file " + file + " is " + input); //Echo the file with the value from the file
			} 
		}
		
	 // This exceptions is thrown if the file is not found
		catch(FileNotFoundException fnfe)
		{
		 	System.out.println("The name of the file you are trying to find is " + file); //Echo the input
			System.out.println("Could not find the file");
			System.out.println("This is a file not found exception");
		}
		
		System.out.println("\n\nEnter an string to change to an integer value");
		value = keyboard.nextLine();  //gather a string to be converted to an integer from the user
	
	//This is trying to convert an the string to an integer
		try
		{
			number = Integer.parseInt(value);
			System.out.println("Input was " + value); //Echo the string
			System.out.println("This value converted to an integer is " + number); //Print the string as an integer
		}
	
	//This exception is thrown because the string could not be converted to a integer
		catch(IllegalArgumentException iae)
		{
		 	System.out.println("The input value was " + value); //Echo the string
			System.out.println("This value can't be converted to an integer");
			System.out.println("This is a number format exception");
		}
	
	//the user is inputing a string and the selecting the position of a character to reprint
		System.out.println("\n\nType in a String"); 
		value = keyboard.nextLine();  //this is the String
		System.out.println("type in the postition of the character that you want to see");
		number = keyboard.nextInt();  //This is the position of the character
		
		System.out.println("The string is " + value +" and the postion of the character is at " + number); //Echo the input
	
	//This is trying to print the charcter at the selected position of the string input by the user
		try
		{
			System.out.println(value.charAt(number));
		}
		
	//This exception was thrown because the character at the desired postion did not exist becuase the string was not long enough
		catch(StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("You tried to print the character at position " + number + " of the string " + value); //Echo the input
			System.out.println("There is no character at this position");
			System.out.println("This is an String Index Out of Bounds exception");
		}
		
	//This is gathering two integers that will be divided
		System.out.println("\n\nInput an integer (dividend)");
		number = keyboard.nextInt();
		System.out.println("Input another integer (divisor)");
		input = keyboard.nextInt();
	
	//This is trying to divide the dividend by the divisor
		try
		{
			System.out.print(number + " divided by " + input + " is " + number / input);
		}
	
	//This exception is thrown because of division by zero
		catch(ArithmeticException ae)
		{
			System.out.println("The dividend is: " + number + " and the divisor is: " + input);  //Echo the input
			System.out.println("Tried to divide by Zero");
			System.out.println("This is an Arithmetic exception");
		}
	
	//This line shows that the program has run succesfully	
		System.out.println("\nThis program has executed successfully and all exceptions have been dealt with");
	}

}

