import java.util.*;
import java.io.*;
/***********************************************
 * Practice working with exceptions and handling.
 *
 * @author - Ryan Tighe
 * @version - V1 - January 17, 2007
 * 2:30 - 3:20 PM
 * Lab 3
 ************************************************/

public class Exceptions
{
	/********************************************
	 * Main Method will run through 5 blocks of code
	 *	looking for exceptions and handling them if needed.
	 *
	 * @param args	command line arguments unused
	 * @exception	FileNotFoundException
	 *	@exception	ArrayIndexOutOfBoundsException
	 *	@exception	NumberFormatException
	 *	@exception	InputMismatchException
	 *	@exception	StringIndexOutOfBoundsException
	 *********************************************/
	public static void main(String args[])
	{
		Scanner keyboard;
		Scanner fileScanner;
		String input; //#1 exc.
		
		int[] array = { 1, 2, 3 }; //#2 exc.
		
		String str = "yoyo";//#3 exc.
		int num; 
		
		int number;//#4 exc.
		number = 0;
		String message;
		
		String in;//#5 exc.
		char cha;
				
		keyboard = new Scanner(System.in);
		
		System.out.println("Exception #1 Check:\n");
		
		//receives input from user about file
		System.out.print("Please input the file you wish to be found and hit return: ");
		input = keyboard.nextLine();
		
		//echos file name
		System.out.println ("The name of the file you want to open is " + input);
		
		
		try // will check to see if File is found
		{
			fileScanner = new Scanner (new File(input)); //reads in file
			
			while (fileScanner.hasNextLine())
			{
				String output;
				
				output = fileScanner.nextLine();
				System.out.println("\nThe value you got from this file is: " + output);
			}
		}
		catch (FileNotFoundException fnfe) //handles exception with error message
		{
			System.out.println ("\nThe file you wanted to open was not found ");
		}
		
		System.out.println("\nException #2 Check: \n");
		
		try //checks for index out of bounds
		{
			for (int x = 0; x <= array.length; x++)
				System.out.println(array[x]);
		}
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("\nThe index of the array has gone outside its bounds.");
		}
		
		System.out.println("\nException #3 Check: \n");
		
		try //tries to convert string to integer
		{
			num = Integer.parseInt(str);
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("\nCould not convert into Integer: " + nfe.getMessage());
		}
		
		System.out.println("\nException #4 Check: \n");
		
		try //checks to see if the input value was an integer
		{
			System.out.print("Please input an integer value: ");
			number = keyboard.nextInt();
		}
		catch(InputMismatchException ime) 
		{
			message = keyboard.nextLine();
			System.out.println("\nThe input value "+ message +" is not a integer.");
		}
		
		System.out.println("\nException #5 Check: \n");
		
		try //looks for a character at an invalid index
		{
			System.out.print("Please input a word: ");
			in = keyboard.nextLine();
			cha = in.charAt(-1);
			System.out.println("The character at -1 is " + cha);
		}
		catch(StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("\nThe index (-1) cannot be located."); 
		}
		
		//allows user to see the program has finished
		System.out.println("\n\nThis program has ended successfully!");
	}
}  
  
