COP 3337 Section U03 Spring 2017 A PROGRAM FOR HW #4 =================== I. The program: --------------- import java.io.FileReader; import java.io.BufferedReader; import javax.swing.JOptionPane; import java.io.IOException; import java.util.StringTokenizer; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Scanner; public class Spring17cop3337TestHw4 { // the number of tries for getting the input/output files private static final int TRIES = 3; public static void main(String[] args) { // display the header printHeader(); // get the input file System.out.println("We get the input file."); BufferedReader in = getInputFile(); // get the output file if (in == null) { System.out.println("Error! You did not enter an input file after " + TRIES + " tries.\nThe program is aborted.\n"); System.exit(1); } // get the output file System.out.println("We get the output file."); PrintWriter out = getOutputFile(); if ( out == null) { System.out.println("Error! You did not enter an output file after " + TRIES + " tries.\nThe program is aborted.\n"); // close the input file try { in.close(); } catch( IOException e) { System.out.println("The input file cannot be closed !"); } System.exit(1); } // process the input file processInputFile(in, out); // close the files try { in.close(); } catch(IOException e) { System.out.println("We cannot close the input file."); } out.close(); } // get the input file private static BufferedReader getInputFile() { // give the user TRIES tries to get the input file BufferedReader inFile = null; String message = "Enter the name of the input file"; for (int i = 0; i < TRIES; i++) { try { String input = JOptionPane.showInputDialog(message); inFile = new BufferedReader(new FileReader(input)); return inFile; } catch (IOException exception) { message = exception + ".\nTry another input file!"; } catch (NullPointerException e) { message = e + ".\nDon't hit Cancel!\nTry again!"; } } return inFile; } // get the outfile private static PrintWriter getOutputFile() { // give the user TRIES tries to get the input file PrintWriter outFile = null; String message = "Enter the name of the output file"; for (int i = 0; i < TRIES; i++) { try { String input = JOptionPane.showInputDialog(message); outFile = new PrintWriter(new FileWriter(input)); return outFile; } catch (IOException exception) { message = exception + ".\nTry another output file!"; } catch (NullPointerException e) { message = e + ".\nDon't hit Cancel!\nTry again!"; } } return outFile; } // process the in file private static void processInputFile(BufferedReader in, PrintWriter out) { // print the error headers System.out.println("\nWe process the input file."); System.out.println("Errors"); System.out.println("======"); int lineNo = 1; boolean errors = false; // true if there are errors Scanner scan = new Scanner(in); // process the input file, line by line while (scan.hasNextLine()) { String line = scan.nextLine(); StringTokenizer tokens = new StringTokenizer(line); if (tokens.countTokens() != 2) { // error System.out.println( " Line " + lineNo + ":" + line + "\nhas " + tokens.countTokens() + " words. It must have 2."); errors = true; } else // process the input line { String first = tokens.nextToken(); String last = tokens.nextToken(); out.println(first.charAt(0) + ". " + last); } lineNo++; } // end while // finish if (!errors) System.out.println("There are no errors."); } // end method // display the header and the preamble private static void printHeader() { System.out.println("Processing a File of Names"); System.out.println("==========================\n\n"); System.out.println("This file reads a file of names of the form " + "\nFirstName LastNames"); System.out.println("It copies the initial of the first name and " + "\nthe last name on the output file as shown below."); System.out.println("FirstNameInitial. LastName"); System.out.println("If the input line does not have 2 words, " + "it is displayed on the monitor \nas an error.\n"); } // end method } // end class II. Run 1: ---------- a. The input file: ------------------ Ali Baba Papa Teddy Bill Karen Poco Pelo Turtleman Leo Gargamel b. The output file: ------------------- A. Baba P. Pelo T. Leo c. The System.out.file: ----------------------- run: Processing a File of Names ========================== This file reads a file of names of the form FirstName LastNames It copies the initial of the first name and the last name on the output file as shown below. FirstNameInitial. LastName If the input line does not have 2 words, it is displayed on the monitor as an error. We get the input file. We get the output file. We process the input file. Errors ====== Line 2: has 0 words. It must have 2. Line 3:Papa Teddy Bill has 3 words. It must have 2. Line 4:Karen has 1 words. It must have 2. Line 6: has 0 words. It must have 2. Line 8:Gargamel has 1 words. It must have 2. BUILD SUCCESSFUL (total time: 20 seconds) III. Run 2: ---------- a. The input file: ------------------ Little Girl Poco Pelo Peter Shrek Papa Bill Papa Smurf Big Tomato b. The output file: ------------------- L. Girl P. Pelo P. Shrek P. Bill P. Smurf B. Tomato c. The System.out.file: ----------------------- run: Processing a File of Names ========================== This file reads a file of names of the form FirstName LastNames It copies the initial of the first name and the last name on the output file as shown below. FirstNameInitial. LastName If the input line does not have 2 words, it is displayed on the monitor as an error. We get the input file. We get the output file. We process the input file. Errors ====== There are no errors. BUILD SUCCESSFUL (total time: 19 seconds) IV. Grading Criteria: --------------------- 1. The errrors are printed in the output file: -3 points 2. The console output has no title: -2 points 3. The lines are not numbered: -1 point 4. Not defining 3 as a constant: -1 point 5. You do not use helper methods. If the program is longer than 1 page you shoud use helper methods: -1 point 6. If there are no errors, there must be a message in System.out stating that there are no errors: -1 point 7. main (and any helper method) must not throw exception: -3 points 8. The feedback to the wrong file name must go to the JOptionpane: -1 point 9. The program ignores the blank lines: -1 point 10. You write that the program found no errors when there are: -1 point 11. You don't get 2 more chances to enter a file name when you hit Cancel: -2 points 12. Run time errors: -2 points