import java.io.*;
import java.util.*;

/********************************************************************
 * Practice file i/o with an application that reads lines from a file 
 * containing three integers and writing them to an output file.
 * @author Nancy Harris
 * @version V1 2/13/2014
 *******************************************************************/

public class FilePreprocess
{
    /*************************************************************
     * The main function for the program described for this class.
     * @param args unused in this application
     *************************************************************/
    public static void main (String [] args)
    {

        String fileName;
        String encoding;
        String goodFileName;
        // String badFileName; // use in Phase 2
        Scanner fileScan = null; 
        PrintWriter goodFileWriter = null;
        
        // get the input file Scanner
        while (fileScan == null)
        {
	        // get input file name from user
	        fileName = getName("Input file name: ");
           
           // get the encoding from the user
           encoding = getName("Get encoding name - UTF-8: ");
	        
	        // create the needed Scanner
	        fileScan = getFileScanner(fileName, encoding);
        }

        // get the output file Printer
        while (goodFileWriter == null)
        {
        	goodFileName = getName("Output file name: ");
        	
        	// create the needed PrintWriter
        	goodFileWriter = getPrintWriter(goodFileName);
        }
        
        // read the data and output the good file data
        readData(fileScan, goodFileWriter);
       
    }
    
    /**************************************************************
     * Prompt the user for the value that we are looking for then
     * read that value in as a line. Return the line entered.
     * @param prompt The prompt to the user
     * @return A string representing the name of the file
     **************************************************************/
    public static String getName(String prompt)
    {
    	return "FileName";
    }
    
    /****************************************************************
     * Get file Scanner will use the file name to build a file object and
     * then will build the Scanner using the character encoding passed as 
     * the second parameter. 
     * 
     * The Scanner using the encoding and File parameters can throw two exceptions.
     * You must catch both of these exceptions and display a message pertinent to 
     * the exception. 
     * 
     * If there is an exception for a bad file the method should print to error output 
     * (System.err):
     *  	"The file, %s, could not be opened." where %s is the name of the file.
     * If there is an exception for the bad argument (character set), the method should
     * print to error output(System.err): 
     * 		"The character set, %s, is not valid." 
     * where %s is the encoding name.
     * 
     * @param fileName The name of the file to build the Scanner over
     * @param charEncoding The character set to use in reading the file
     * @return A Scanner object or null if the Scanner could not be created
     *****************************************************************/
    public static Scanner getFileScanner(String fileName, String charEncoding)
    {
    	return null;
    }
    
    /************************************************************************************
     * getPrintWriter will return a PrintWriter object over the file name.
     * If the PrintWriter cannot be created, print the message,
     * 	"The file, %s, could not be opened." where %s is the name of the file.
     * @param fileName The name of the file to write
     * @return The PrintWriter to write the file to or null if it cannot be created.
     ************************************************************************************/
    public static PrintWriter getPrintWriter(String fileName)
    {
    	return null;
    }
    
    /************************************************************************************
     * readData reads the data from an input file, checks to see that it is correct and
     * writes correct data to the output file.
     * @param fileScan The input Scanner to read from
     * @param goodWriter The output PrintWriter to write to
     ************************************************************************************/
    public static void readData(Scanner fileScan, PrintWriter goodWriter)
    {
    	
    }
    
    /**************************************************************************************
     * checkLine checks to see that the input line is correct for this file.
     * A correct line has three integers separated by commas. If the input line is null or 
     * the empty String return false.
     * 
     * @param input The input line to check
     * @return true if the line contains three integers separated by commas, false otherwise
     **************************************************************************************/
    public static boolean checkLine(String input)
    {
    	String [] tokens;
    	boolean isOk;
    	
    	isOk = true;
    	
    	if (input != null && input.length() > 0)
    	{
    		tokens = input.split(",");
    		if (tokens.length == 3)
    		{
    			for (String ss : tokens)
    			{
    				try 
    				{
    					Integer.parseInt(ss);
    				}
    				catch(NumberFormatException nfe)
    				{
    					isOk = false;
    				}
    			}
    		}
    		else
    		{
    			isOk = false;
    		}
    	}
      else 
      {
         isOk = false;
      }
      
    	return isOk;
    }
}
