Fall 2013 CSCI 202 Homework 5

This assignment must be completed by 10:10 AM on Saturday, 28 September and uploaded to the Homework 5 moodle page. You should upload only your Home05.java file.

Your program should be implemented in a class named Home05 in edu.unca.cs.csci202 package and have a proper @author annotation.


This assignment tests file I/O and exception handling.

The task

Subtask One

The first thing your program should do is read a single line of input from System.in . (You real should use a Scanner for this.)

Your program will trim the string of leading and trailing white space and then try to open an output file using the input string as the name of the file. If the open fails, your program should terminate by writing a message similar to the following to the System.err .
Unable to open output file: FILENAME
where FILENAME is the filename typed by the person running the program.

Subtask Two

Now the program continues by reading a list of lines corresponding to post offices and ZIP codes. A comma will always separate the city from the state. You may assume there are no extra spaces within the city name. Here’s an example of legal input for the program.

Asheville, NC 28804
  Asheville,   NC   28801
 Asheville  ,   NC   28806
New Bern, NC 28561
Truth or Consequences, NM 87901  

In the output, the lines should be neatly printed as shown below with 20 spaces set aside for the city. In the city name is longer than 20 characters, stop at the 20th. Here’s an example of the output for the above input.

Asheville            NC 28804
Asheville            NC 28801
Asheville            NC 28806
New Bern             NC 28561
Truth or Consequence NM 87901  

The program should stop printing addresses when it encounters an improperly formatted line, for example, a blank line or a line with no comma.