// inport needed classes from packages
import java.util.Scanner;
import java.io.*;

public class Lab6
{
   public static void main (String [] args)throws IOException
	{
 
      // declarations of needed variables and objects
		char [ ] letters;                        // character array
		String inputString; 							  // input String	
		Scanner fileScanner, keyboardScanner;    // file and keyboard Scanners
		String inputFileName, outputFileName;    // input and output filenames
		boolean inputFileFound;                  // boolean telling whether file found
		int inputStringLength;						  // integer String length
		FileWriter myFileWriter;					  // FileWriter	
	   BufferedWriter myBufferedWriter;         // BufferedWriter
	   PrintWriter 	myPrintWriter;            // Print Writer
					
		// initialize needed variables
		inputFileFound = false;

		
		// instantiate keyboardScanner
		keyboardScanner = new Scanner (System.in); 
		
		// get filenames from user
		System.out.println (" please enter the name of your input data file ");
		inputFileName = keyboardScanner.nextLine();
				
		System.out.println (" please enter the name of your output data file ");
		outputFileName = keyboardScanner.nextLine();
		
		// instantiate input file and keep trying until successful
		do
		   try
		  {
		     fileScanner = new Scanner (new File (inputFileName));
			  inputFileFound = true;
        }
		  catch (FileNotFoundException fnfe)
		  {
		     fileScanner = new Scanner (new File ("squib.txt"));
		     System.out.println (" your file could not be found ");
			  System.out.println (" please enter another name ");
			  inputFileName = keyboardScanner.nextLine();
		  }
	   while (!inputFileFound);
		
		   // get data from file
		inputString = fileScanner.nextLine();
	
		// instantiate Objects needed to output to a  file    
      myFileWriter = new FileWriter (outputFileName);
	   myBufferedWriter = new BufferedWriter (myFileWriter);
	   myPrintWriter = new PrintWriter (myBufferedWriter);
				
		// echo the input String to the screen & to your output file
		System.out.println (" the input String was:    " + inputString);
		myPrintWriter.println (" the input String was:   " + inputString);
		
		//  determine input String's length & output it to screen & output file
		inputStringLength = inputString.length();
		System.out.println 
		    (" the length of the input string is " + inputStringLength);
		myPrintWriter.println 
		    (" the length of the input string is " + inputStringLength); 

		/*  
		    output the characters in the input String to file & screen, 1 per line
		    preeceded by the following statement: 
		    Here are the input file characters printed using a standard FOR statement 
      */
		  
           // print the header statements
      System.out.println 
		("  Here are the input file characters printed using a standard FOR statement ");
		myPrintWriter.println
		("  Here are the input file characters printed using a standard FOR statement ");

			 // print the characters in the String
		for (int i = 0; i < inputStringLength; i++)
		{
		  System.out.println (inputString.charAt(i));
		  myPrintWriter.println (inputString.charAt(i));
		}
	
	   // print a blank line to the screen and to the file
		System.out.println ();
		myPrintWriter.println ();
		
		// transform the inputString into an array using the String Method
		letters = inputString.toCharArray();
	
		/* using the alternate form of a FOR loop described on pages 249, shown on page 251 and
		   on page 384  print the letters in the array on a single line
		   preceded by the following statement
		   Here are the character in the array printed using the foreach loop form
		*/
		
			// print the header statements	
		System.out.print 
    		("  Here are the input file characters printed using a foreach statement:  ");
		myPrintWriter.print
   		("  Here are the input file characters printed using a foreach statement: ");

		   // print the characters in the array using the foreach loop form
		for ( char theLetters : letters)
		{
		   System.out.print (theLetters);
			myPrintWriter.print (theLetters);
		}
		
		// print two blank lines
		System.out.println();
		
		// close the output file
		myPrintWriter.close();
		
		// print out the names of the input and output files
		System.out.println ( " The input file used was : " + inputFileName);
		System.out.println ( " The output file created was : " + outputFileName);
	}
}
	 
		    