import java.util.Scanner;
import java.io.*;
/**********************************************
 * ExceptionsLab.java
 * This program causes 5 different exceptions
 * and handles them all.
 *
 * Krys Stultz
 * 1/17/07
 * Lab 3
 * Section 2
 *
 * @author Krys Stultz
 * @version v1.0
 *
 * Exceptions used:
 * 1) ArrayIndexOutOfBounds
 * 2) FileNotFoundException
 * 3) ArithmeticException
 * 4) NumberFormatException
 * 5) StringIndexOutOfBounds
 *********************************************/
 
public class Exceptions_Lab_stultzkm_ExceptionsLab
{
	public static void main(String args[])
	{
		// Create an array with 5 elements
		int[] num = {1, 2, 3, 4, 5};
		
		try
		{
			// Output contents of array
			for(int i = 0; i <=5; i++)
				System.out.println("Contents of array: " + num[i]);
		}
		catch(ArrayIndexOutOfBoundsException aioobe)
		{
				System.out.println("Array index out of bounds.\n");
		}
		
		// Create an instance of Scanner to read from a file and declare other variables
		Scanner fileScanner;
		String file;
		file = "C:\\test.txt";
		int value;
		
		try
		{
			// Echo the file we are looking for
			System.out.println("Looking for file " + file);
			
			fileScanner = new Scanner (new File (file));
			
			// Print values that are in the file until there are no more
			while (fileScanner.hasNextInt()) 
         {
         	value = fileScanner.nextInt();
         	System.out.println (" The value is: " + value);
         }
		}
		catch(FileNotFoundException fnfe)
		{
			System.out.println("Specified file not foud.\n");
		}
		
		// Declare variables to do an operation
		int solution, operand1, operand2;
		operand1 = 100;
		operand2 = 0;
				
		try
		{
			// Output contents of operands
			System.out.println("Dividing " + operand1 + " by " + operand2);
			
			// Set solution equal to operand1 divided by operand2
			solution = operand1 / operand2;
		}
		catch(ArithmeticException ae)
		{
			System.out.println("Arithmetic Exception error.\n");
		}
		
		// Declare and initialize a string variable
		String str;		
		str = "Not an Integer";
		int strConv;
		
		try
		{
			// Output contents of str
			System.out.println("The contents of str is: " + str);
			
			// Convert the contents of str to an integer
			strConv = Integer.parseInt(str);
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("Number format exception, target variable not an integer.\n");
		}
		
		// Declare a string and ask the user for input
		String myString;
		
		Scanner keyboard;
		keyboard = new Scanner(System.in);
		
		System.out.print("Enter a string: ");
		myString = keyboard.nextLine();	
		
		System.out.println("\nContents of myString is: " + myString);
		
		try
		{
			// Attempt to get the 1024th character of myString and print it
			System.out.println("1024th character of the string is: " + myString.charAt(1024));
		}
		catch(StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("String index out of bounds.\n");
		}
				
		System.out.println("Program ended successfully.");
	}
}