/************************************************
* Lab3.java
*
* @author:  Robert MacHardy
* Date:		01/26/05
*
*/

//************************************************
// Honor Statement:  This work conforms to the JMU
//		Honor Code and the academic rules for this class.
//************************************************

import java.util.Scanner;
import java.io.*;

public class Lab3
{
	/** main method - reads from a text file named by the user
	*	 Outputs the lowest value scanned and the number of times 13 was scanned
	*	 Catches exceptions that may occur
	*/
   public static void main (String args []) throws FileNotFoundException
	{
		Scanner myKeyboardScanner, myFileScanner;
      String fileName, input;     
		boolean fileFound, intFound;
		int number, minInt, num13count;
	   
		// sets up myKeyboardScanner for keyboard input
		myKeyboardScanner = new Scanner (System.in);
		
		//Avoid may not be initialized error with a dummy
		myFileScanner = new Scanner ("dummy");
		fileFound = false;
		
		do // repeat until a file is found
		{
			System.out.print("Input the name of a file to scan from: ");
			fileName = myKeyboardScanner.nextLine();
		
			try
			{
				myFileScanner = new Scanner (new File (fileName));	// remember the "" and \\
				fileFound = true;
			}
			catch (FileNotFoundException fnfe)
			{
				System.out.println("File Not Found");
			}
		}while (!fileFound);
		
		minInt = Integer.MAX_VALUE; // any input except for the max will be lower
		num13count = 0;
		input = "";	
		intFound = false;	
		number = Integer.MIN_VALUE;
		
		if (myFileScanner.hasNext())
		{
			Scanner lineScan;
			while (myFileScanner.hasNext())
			{
				
				input = myFileScanner.nextLine();
				lineScan = new Scanner(input);
				while (lineScan.hasNext())
				{
					input = lineScan.next();
					try
					{		
						number = Integer.parseInt(input);
						intFound = true;			
						System.out.println("Number scanned: " + input);
						if (number < minInt)
							minInt = number;
						if (number == 13)
							++num13count;
					}
					catch (NumberFormatException nfe)
					{
						System.out.println("Scanned " + input + " is not an integer");
					}					
				}
			}		
			
			if (intFound)
			{	
				System.out.println("\nLowest integer scanned: " + minInt);	
				System.out.println("Number of times 13 was scanned: " + num13count);	
			}
			else
				System.out.println("No integers scanned from file");
		}
		else
			System.out.println("Text file was empty");
	}
}

      