/************************************************
* This class, Lab3.java ...
*
* @author:  Elizabeth Adams
* Date:		01/26/05
*
*/

//************************************************
// Honor Statement:  This work conforms to the JMU Honor Code and 
// the academic rules for this class.
// Acknowledgement:  
//************************************************

import java.util.Scanner;
import java.io.*;

public class Lab3_13a
{
   public static void main (String args []) throws FileNotFoundException
	{
		Scanner myKeyboardScanner, myFileScanner;
      String fileName;     

	   boolean fileFound;
	   int smallest;
		boolean numberFound;
	   int  thirteenCount;
	   int temp;
		String junk;
			
    		// sets up myKeyboardScanner for keyboard input
		myKeyboardScanner = new Scanner (System.in);
		
            // necessary initializations	   
		fileFound = false;     // file hasn't been found yet
		numberFound = false;   // a number hasn't been found yet
      thirteenCount = 0;     // no 13s have been found yet
		smallest = Integer.MAX_VALUE; // because smallest used in an if statement
				
		do
		{
	               // prompt user for filename
		   System.out.println 
			   ("Please enter the name of your file with absolute pathname");
	
	             // get a fiename from the user
		   fileName = myKeyboardScanner.nextLine();
			 
	      try      // to use the user's filename
	   	{  
		      myFileScanner = new Scanner (new File (fileName ) );	
            fileFound = true;               // was successful
         }
		   catch (FileNotFoundException fnfe) // was unsuccessful
			{
		       System.out.println 
				    (" the file whose name you entered could not be found ");
				 myFileScanner = new Scanner (new File ("squib.txt"));
		   }
		}	

		while (!fileFound);  // try again if unsuccessful
		
		    // while there is more data in the file
		while ( myFileScanner.hasNext())
		{
		    try     // to get a number
			 {
			   temp = myFileScanner.nextInt();
				if (!numberFound)   // is this is the first number obtained
				{
				   smallest= temp;
				   numberFound = true;
				}
				else if (temp < smallest)  // is it a new smallest
				   smallest = temp;
				if (temp == 13)            // is it 13?
				   thirteenCount++;
				System.out.println (" integer " + temp + " found ");
			 }			      
			 catch (NumberFormatException nfe)
			 {
			   junk = myFileScanner.next();  // bypass non-integer
				System.out.println (" non-integer " + junk + " found ");
			 }
		
			 catch (java.util.InputMismatchException ime)
			 {
			   junk = myFileScanner.next();  // bypass non-integer
				System.out.println (" non-integer " + junk + " found ");
			 }
      
	} // end while
		
		if (!numberFound)
		   System.out.println(" there were no integers in the file");
		else 
		{
		   System.out.println 
			   (" The smallest number in the file was " + smallest);
			System.out.println
			   (" The number of thirteens in the file was: " + thirteenCount);
		}
				 
	}// end main
	
}// end class