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.
 **/

public class FilePreProcessV1
{
    /**
     * 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 goodFileName;
        // String badFileName;
        Scanner fileScan = null; 
        PrintWriter goodFileWriter = null;
        
        while (fileScan == null)
        {
	        // get input file name from user
	        fileName = getFileName("Input file name: ");
	        
	        // create the needed Scanner
	        fileScan = getFileScanner(fileName, "UTF-8");
        }

        while (goodFileWriter == null)
        {
        	goodFileName = getFileName("Output file name: ");
        	
        	// create the needed PrintWriter
        	goodFileWriter = getPrintWriter(goodFileName);
        }
        
        // read the data and output the good file data
        readData(fileScan, goodFileWriter);
        
        // display the two output files.  
        
    }
    
    /**************************************************************
     * @param prompt The prompt to the user
     * @return A string representing the name of the file
     **************************************************************/
    public static String getFileName(String prompt)
    {
    	String fileName;
    	Scanner keyboard;
    	
    	keyboard = new Scanner (System.in);
    	
    	System.out.print(prompt);
    	fileName = keyboard.nextLine();
    	
    	return fileName;
    }
    
    /****************************************************************
     * @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)
    {
    	File inFile;
    	Scanner fileScan;
    	
    	inFile = new File(fileName);
    	
    	try
    	{
    		fileScan = new Scanner (inFile, charEncoding);
    	}
    	catch (FileNotFoundException fnfe)
    	{
    		fileScan = null;
    		System.out.println("The file: " + inFile.getName() + " could not be opened");
    	}
    	catch (IllegalArgumentException iae)
    	{
    		fileScan = null;
    		System.out.println("The character set: " + charEncoding + " is not valid.");
    	}
    	
    	return fileScan;
    }
    
    /************************************************************************************
     * @param fileName The name of the file to write
     * @return The PrintWriter to write the file
     ************************************************************************************/
    public static PrintWriter getPrintWriter(String fileName)
    {
    	File outFile;
    	PrintWriter writer;
    	
    	outFile = new File(fileName);
    	
    	try
    	{
    		writer = new PrintWriter(outFile);
    	}
    	catch (FileNotFoundException fnfe)
    	{
    		writer = null;
    		System.out.println("The file, " + outFile.getName() + ", could not be opened.");
    	}
    	
    	return writer;
    }
    
    /************************************************************************************
     * readData reads the data from an input file, checks to see that it is correct and
     * writes correct data to the output file.
     ************************************************************************************/
    public static void readData(Scanner fileScan, PrintWriter goodWriter)
    {
    	String input;
    	boolean lineOk;
    	
    	while (fileScan.hasNext())
    	{
    		input = fileScan.nextLine();
    		lineOk = checkLine(input);
    		
    		if (lineOk)
    		{
    			goodWriter.println(input);
    		}
    		else
    		{
    			System.err.println("Bad line: " + input);
    		}
    	}    	
    }
    
    /**************************************************************************************
     * 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;
    		}
    	}
    	return isOk;
    }
}
