Lab 14 Name: Checked: Objectives: Practice handling exceptions and writing text files. Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab. 1. Review your code for Lab 11, DeckOfCards.java. Save a copy in your Lab 14 folder. 2. Modify the code so that it creates an output text file containing the cards of the deck, after they are shuffled. Reminders: Prompt the user to enter a file name. Create a PrintWriter object named outfile, using the name that the person entered as the filename. Modify the code so that instead of using System.out.println( ) it uses outfile.println( ) to output to the file. Don t forget to close() the file at the end (after all output to the file is done) Don t forget to import java.io.*; Remember to add throws IOException in the main() method heading. 3. Run the code three times, obtaining three text files. 4. Submit the three output files along with your code.
Part A: try/catch NumberFormat 1. Using the code for ResponseTimeExperiment, experiment with bad inputs until you cause NumberFormatException. Note the error message generated: 2. Is NumberFormatException a checked or unchecked exception? 3. With your partner, look up NumberFormatException in our lecture slides and the Java API. Note the classes in between: Exception NumberFormatException 4. Create a try block around the code that parses a response and uses it to compute the outcome. Use a catch clause for NumberFormatException that prints "FAIL: You were supposed to type in a number". 3. Add some more code in the catch clause so that in cases where the response was invalid, the output file will contain an asterisk ( * ) for that input. For example, the output file corresponding to a particular run might look as follows, indicating that for the 3 rd question the subject entered a non-numeric answer (as opposed to an incorrect answer). 1 2529 0 1875 * 1 2773
Part B: IOException 1. What if the file cannot be created? Can you figure out how to crash your program (ie, cause a kind of IOException to be thrown (most likely FileNotFoundException) This can happen if the name entered by the user cannot be used by your operating system to create a file with that name. Play around with the program using different inputs for your name (Hint: try various symbols and non-alpha characters), until you cause this exception and make a note of the input here: Bad file name that crashed the program: Message: 2. Now let s try/catch it: First, separate the PrintWriter declaration from the instantiation: PrintWriter outfile; // declaration outfile = new PrintWriter(... ); // instantiation Next, place the instantiation for the PrintWriter in a try block, and catch IOException (that will catch FileNotFoundException and others as well). In the catch, use the following code to handle the exception: outfile = new PrintWriter(System.out); print a warning on standard output, such as: WARNING: Unable to create file (*name of file*). Data output to System.out. To summarize: The experiment proceeds as before. If no IOException occurs, the file will be created; if that is impossible (i.e., an exception occurs), the program will recover from the exception: 1) issue a warning and 2) proceed as before, but the contents that would have normally gone to the file will instead be displayed on the screen. Part C: What if your subject is prone to obscenity? Sometimes programs need to respond to rude or offensive user behavior. Here we will handle this situation by throwing an exception, so that the program aborts when the user enters a forbidden word as their name. In the DeckOfCards program, we will check that the file name entered is not a banned word before creating a file with the name. Follow these steps: 1. Create a class BadWordException.java that extends Exception. Here is the code: //******************************************************************** // BadWordException.java Author: M A Papalaskari
// Represents an exceptional condition for using bad words. //******************************************************************** public class BadWordException extends Exception //----------------------------------------------------------------- // Sets up the exception object with a particular message. //----------------------------------------------------------------- BadWordException(String message) super(message); Save this code in your Lab 14 folder. Compile it. (You should not try to run it.) 2. Create a file excusemyfrench.txt of banned words. Try first with just a few words (such lists are available free on the web, you can download one later). 3. In the new version of DeckOfCards.java, in the heading of the main() method add: throws BadWordException, IOException 4. Now in DeckOfCards.java we can check that the name entered is not a banned word. We will do this after obtaining the name from the user, by running through the banned words file, comparing the name with each word in the file. If a match is found, we punish them by throwing an exception! (i.e., the program terminates). Here is some code to do this: Scanner badwords = new Scanner(new File("excusemyfrench.txt")); /**/ while (badwords.hasnext()) if (name.equals(badwords.next())) throw (new BadWordException("Watch your tongue")); 5. Note that we are also faced with the possibility of an IOException, this time for the input file excusemyfrench.txt (it may not exist or may not be accessible for some reason). Move the file temporarily to a different folder and run the program again to observe what happens. 6. Let s handle the problem of this missing file. Place the code that sets up the Scanner and checks for matches (code given in 4, above) inside a try block with a catch clause that issues a warning and omits the check for bad words: catch (IOException e) System.out.println("Warning: excusemyfrench.txt missing. "); System.out.println("Not checking input for obscenity"); In other words, if the file excusemyfrench.txt is missing, the program will function normally, except that it will not be checking for banned words.
Lab 14 Comments Name: Comments on this lab, please: What was the most valuable thing you learned in this lab? What did you like best about this lab? Was there any particular problem? Do you have any suggestions for improving this lab as an effective learning experience?