/************************************************
* This class, Lab5a.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:  Indicate any student help or other 
//		references for this program not directly given in the
//		assignment.  Include TA name if applicable.
//************************************************

   import java.util.Scanner;
   import java.io.*;

    public class Lab5a
   {
   /** main method - what do I do?
   */
       public static void main (String args []) throws IOException
      {
      // These are required variables.  Add any other variables needed
      // below these required ones.  
         Scanner myKeyboardScanner, myFileScanner, lineScanner;
         boolean fileFound, numberFound;
         String inputFileName, goodOutputFileName,badOutputFileName;     
         String dataLine;
         String badValue;
         int goodValue;
         FileWriter myGoodFileWriter;
         BufferedWriter myGoodBufferedWriter;
         PrintWriter myGoodPrintWriter;
         FileWriter myBadFileWriter;
         BufferedWriter myBadBufferedWriter;
         PrintWriter myBadPrintWriter;
         int smallest ;
         int thirteenCount, valueCount ;
         
            // sets up myKeyboardScanner for keyboard input
         myKeyboardScanner = new Scanner (System.in);
      
            // prompt user for input file name
         System.out.println 
            ("Please enter the name of your input file with absolute pathname");
      
           // get input filename from the user
         inputFileName = myKeyboardScanner.nextLine();
       
         fileFound = false;   // input file hasn't been found yet
      
         do
         {
            try
            {  
               // to use the user's filename
               myFileScanner = new Scanner (new File (inputFileName ) );	
               fileFound = true;  // was successful
            }
                catch (FileNotFoundException fnfe) // failed
               {
                  System.out.println 
                     (" the file whose name you entered could not be found ");
                  myFileScanner = new Scanner ("squib.txt");
                  System.out.println  // re-prompt user for input file name
                     ("Please enter the name of your input file with absolute pathname");
                  inputFileName = myKeyboardScanner.nextLine(); // gets input filename from the user
               }
         }	
         while (!fileFound);  // try again if unsuccessful
      
            // prompt user for output file name for good data
         System.out.println 
            ("Please enter the name of your output file for good data with complete path name");
      
           // get output filename from the user for good data
         goodOutputFileName = myKeyboardScanner.nextLine();
      
             //prepare output file for writing good data
         myGoodFileWriter     = new FileWriter(goodOutputFileName);		 
         myGoodBufferedWriter = new BufferedWriter (myGoodFileWriter);
         myGoodPrintWriter    = new PrintWriter (myGoodBufferedWriter);
      
            // prompt user for output file name for bad data
         System.out.println 
            ("Please enter the name of your output file for bad data with complete path name");
      
           // get output filename from the user for bad data
         badOutputFileName = myKeyboardScanner.nextLine();
      
             //prepare output file for writing good data
         myBadFileWriter     = new FileWriter(badOutputFileName);		 
         myBadBufferedWriter = new BufferedWriter (myBadFileWriter);
         myBadPrintWriter    = new PrintWriter (myBadBufferedWriter);
      
         while ( myFileScanner.hasNext()) // while there is more data in the input file
         {
            dataLine = myFileScanner.nextLine();  // reads the line
            System.out.println (" picked up " +  dataLine );
            lineScanner = new Scanner (dataLine); // creates the line Scanner
           //lineScanner.useDelimiter(" ";
            while (lineScanner.hasNext()) // there is more data on the line
            {
               try // to get a number
               {
                  goodValue = lineScanner.nextInt(); // gets a number
                  System.out.println (goodValue + " integer ");
                  myGoodPrintWriter.println (goodValue);
               }
                   catch (java.util.InputMismatchException ime)
                  {
                     badValue = lineScanner.next();  // gets a String from line
                     System.out.println (badValue + " String");
                     myBadPrintWriter.println (badValue);
                  }
            } // end inner while
         } // end outer while
         myGoodPrintWriter.close(); 
         myBadPrintWriter.close();
         
         System.out.println (" STARTING PART 2 ");
      			// initialize variables for second part
      			
      			
         smallest = Integer.MIN_VALUE;
         thirteenCount = 0;
         fileFound = false;
         numberFound = false; 
         valueCount = 0;	
      									   	
      	   // prompt user for input file name
         System.out.println 
            ("Please enter the name of your input file with absolute pathname");
      
           // get input filename from the user
         inputFileName = myKeyboardScanner.nextLine();
         
         do 
         {
            try
            {
               myFileScanner = new Scanner (new File (inputFileName));
               fileFound = true;
            }
                catch (FileNotFoundException fne)
               {
                  System.out.println (" your file wasn't found ");
                  System.out.println (" please enter your input filename again ");
                  myFileScanner = new Scanner ("junk.txt");
                  inputFileName = myKeyboardScanner.nextLine();
               }
         }		 
         while (!fileFound);  
      	     
      		    
         while ( myFileScanner.hasNext())  // process data
         {
            try // to get a number
            {
               goodValue = myFileScanner.nextInt();
               if (!numberFound)
               {
                  smallest= goodValue;
                  numberFound = true;
               }
               else if (goodValue < smallest)  // is it smallest
                  smallest = goodValue;
            
               if (goodValue == 13)      // is it 13?
                  thirteenCount++;
               valueCount++;
            
            	// System.out.println (" integer " + temp + " found ");  // for debugging only
            }       
            catch (java.util.InputMismatchException ime)
            {
                  badValue = myFileScanner.next();  // bypass non-integer
                  System.out.println (" non-integer " + badValue + " found ");
                   valueCount++;
             }
         } // 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);
            System.out.println
                 	( " The number of values in the file was: " + valueCount);}  
       	
      } // main  
   }// end class