/************************************************
* This class, Lab5.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 Lab5
   {
   /** 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;
         String inputFileName, goodOutputFileName,badOutputFileName;     
         String dataLine;
         String badValue;
         int goodValue;
         FileWriter myGoodFileWriter;
         BufferedWriter myGoodBufferedWriter;
         PrintWriter myGoodPrintWriter;
         FileWriter myBadFileWriter;
         BufferedWriter myBadBufferedWriter;
         PrintWriter myBadPrintWriter;
      
      
            // 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 ( dataLine + " picked up ");
            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 + " integer ");
               }
                   catch (java.util.InputMismatchException ime)
                  {
                     badValue = lineScanner.next();  // gets a String from line
                     System.out.println (badValue + " String");
                     myBadPrintWriter.println (badValue + " String");
                  }
            } // end inner while
         } // end outer while
         myGoodPrintWriter.close(); 
         myBadPrintWriter.close();
      } // main  
   }// end class
	
