import java.util.*;
import java.io.*;
/*******************************************************
 * @author Murat Akhmetov
 * @version V1 - 01/17/07
 * Section 0002
 * Lab3
 *
 *******************************************************************
 * The program demonstrates handling exceptions by try-catch blocks.
 *******************************************************************
 *
 * The exceptions handled in the program:
 * 1) InputMismatchException
 * 2) ArrayIndexOutOfBoundsException
 * 3) FileNotFoundException
 * 4) NumberFormatException
 * 5) StringIndexOutOfBoundsException
 *******************************************************/ 
 public class Exceptions_Lab_akhmetma_ExceptionsLab
 {
	 public static void main(String [] args)
	 {
	 	Scanner keyboard = new Scanner(System.in);
		int [] array = {2, 3, 4, 5};// to show ArrayIndexOutOfBoundsException
		String fileName = "Murat.txt";// file name for FileNotFoundException
		String str = "abc";
		
		// tries to input an integer from keyboard
		// and throws InputMismatchException
		try
		{
			System.out.print("Enter an integer: ");
			int num = keyboard.nextInt();
		
			System.out.println(num);// echos the integer if it
			                        // is inputed correctly
			System.out.printf("\n");
		}
		catch (InputMismatchException e)
		{
			System.out.printf("The 1st exception: "
			                  + "InputMismatchException\n");
			System.out.printf("You did not enter an integer!\n\n");
		}
		
		// tries to print out elements of an array and
		// throws ArrayIndexOutOfBoundsException
		try
		{
			System.out.println("Printing elements of an array:");
			for (int i = 0; i <=4; i++)
			{
				System.out.print("Element #" + i + ": ");
				System.out.println(array[i]);
			}
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.printf("\nThe 2nd exception: "
			                  + "ArrayIndexOutOfBoundsException\n");
			System.out.printf("Index of an array element is out of bounds" 
			                   + " of the array!\n\n");
		}
		
		// tries to read a file and throws FileNotFoundException
		try
		{
			System.out.println("Trying to open file " + fileName + "...");
			FileReader freader = new FileReader(fileName);
		}
		catch (FileNotFoundException e)
		{
			System.out.printf("The 3rd exception: "
			                  + "FileNotFoundException\n");
			System.out.printf("The file %s is not found!\n\n", fileName);
		}
		
		// tries to convert a number from string format to an integer
		// by using parseInt method and throws NumberFormatException
		try
		{
			System.out.printf("This is an integer: %d", Integer.parseInt(str));
		}
		catch (NumberFormatException e)
		{
			System.out.println("Trying to convert the string \""
			                   + str + "\" into an integer...");
			System.out.printf("The 4th exception: NumberFormatException\n");
			System.out.printf("An invalid string for parseInt method!\n\n");
		}
		
		// tries to print 10th character of a string and
		// throws StringIndexOutOfBoundsException
		try
		{
			System.out.println("Trying to print 10th character "
			                   + "in a string \"" + str + "\"...");
			System.out.println(str.charAt(10));
		}
		catch (StringIndexOutOfBoundsException e)
		{
			System.out.printf("The 5th exception: "
			                  + "StringIndexOutOfBoundsException\n");
			System.out.printf("There is not such a character "
			                  + "with an index of 10!\n\n");
		}
		
		// prints out ending message
		System.out.println("\nThe program has ended successfully!\n");
	 }
 }