CSCI 202 in-class lab 3

Fall 2013 CSCI 202 in-class lab 3

Attribution and Copyright

This lab description was written for the Big Java textbook and slightly modified for this lab. © John Wiley & Sons, Inc.

Getting started

Create a NetBeans project for a Java application. Call the main class of your application ZipsReader.

Download the file ziptable.txt and store it in the project directory. (The instructor will show you how to do this.) Each line of the table contains a state abbreviation, the state name, and one or more ZIP code specifiers. The ZIP code specifiers consist of the first three digits of a ZIP code. For example 350-369 denotes ZIP codes in the range 35000 to 36999.

Tables like this one are used in mailing applications to verify that an address has a reasonable ZIP code. Each ZIP code specifier is either a pair of three-digit integers separated with a hyphen, or a single three-digit integer. A ZIP code for a state is “valid” if the first three digits falls in the range of a ZIP code specifier pair or matches a single three-digit ZIP code specifier.

In order to standardize the data in the table, we will create a new file by modifying the old table data so that every ZIP code specifier is a pair of integers separated with a hyphen. For example, in the case of New York, we will change the single ZIP code 005 into 005-005. We will divide the task into three steps.

Step 1

As the first step, write a program that reads and prints each line in the table exactly as it appears in the file.

Part 2

Modify the ZipsReader program so that it passes each line of the table file as a String to another Scanner constructor. Use the new Scanner to read the single line of the table as a sequence of strings by invoking the next method. (You may want to ask the instructor about this.) Print out the state name, state abbreviation, and each ZIP code specifier.

In order to make the output consistent, single integer ZIP code specifiers should be printed as a two-integer range. For example, New York’s ZIP code of 005 should be printed as 005-005. There are a couple of hurdles to completing this:

Fortunately, the String class supports the matches method and we can use that method to determine if a string matches a specific pattern. We will denote the pattern as a regular expression. For example, if we have a String identifier called token, we can invoke token.matches("\\d{3}-\\d{3}") to make sure the string referenced by token consists of three digits followed by a hyphen followed by three more digits. We can use token.matches("\\d{3}") to make sure the string referenced by token consists of exactly three digits. In each case, matches returns true or false, so we can use this expression in an if statement to detect when we have scanned a ZIP code specifier.

Part 3

Modify the ZipsReader program by directing the output to a file rather than System.out. Create a PrintWriter object that is associated with a file for output. Use print and println to write your output to the file. Be sure to invoke close on the stream when you are finished. Open the output file in NetBeans and verify that it is correct.