/*************************************
* Test the try and catch of 5 different Exceptions
* they are:
* ArithmaticException
* FileNotFoundException
* MalformedURLException
* StringIndexOutofBounds
* NumberFormatException
*
* @author - Andrew Smith
* @version - V1 - 1/17/07
**************************************/
import java.util.Scanner;
import java.io.*;
import java.net.*;

public class Exceptions_Lab_smith3at_Exceptions
{
	/*********************************
	* main method
	*
	*********************************/
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);

		/************************************************
		* Tests the division of two integer values and if
		* division by zero occurs, prints the exception.
		*
		**************************************************/
		try
		{
			int value1, value2, total;

			System.out.println("Enter in number value 1: ");
			value1 = keyboard.nextInt();
			System.out.println("Enter in number value 2: ");
			value2 = keyboard.nextInt();

			total = value1/value2;

			System.out.println("The value of " + value1 + " divided by " + value2 + " is " + total);
		}
		catch(ArithmeticException ae)
		{
			System.out.println("Cannot perform this operation, division by zero.");
		}

		/************************************************
		* Tests to see if the file exists, if it does open it
		*
		**************************************************/
		try
		{
			String file;
			String str;

			System.out.println ("\nEnter the file name ");
			keyboard.nextLine();
			file = keyboard.nextLine();

			Scanner filereader = new Scanner(new File (file));

			System.out.println ("The file is " + file);
			while (filereader.hasNextLine())
			{
				str = filereader.nextLine();
				System.out.println (" The value you got from the file is " + str);
			}
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println (" The file you wanted to open was not found ");
		}

		/**************************************
		* checks if the given url's exist, the first one works
		* the second fails and is caught
		*
		****************************************/
		try
		{
			URL url1 = new URL("https://users.cs.jmu.edu/adamses/web/");
			URL url2 = new URL("www.asasdg.com");

			System.out.println(url1 + " exists");
			System.out.println(url2 + " exists");
		}
		catch(MalformedURLException mURLe)
		{
			System.out.println("\nThe site www.asasdg.com does not exist");
		}

		/************************************************
		* Tests to see if the input string has a 7th character
		*
		**************************************************/
		try
		{
			String str;
			System.out.println("\nEnter in a string value of 7 characters or more: ");
			str = keyboard.nextLine();

			System.out.println("The 7th character is " + str.charAt(6));
		}
		catch(StringIndexOutOfBoundsException sioobe)
		{
			System.out.println("The string entered is not long enough.");
		}


		/************************************************
		* Tests to see if the input is an integer
		*
		**************************************************/
		try
		{
			String str;
			int input;

			System.out.println("\nEnter in an integer value: ");
			str = keyboard.nextLine();

			input = Integer.parseInt(str);

			System.out.println("The value entered was a " + input);
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("\nThe value entered is not an integer.");
		}
	}
}