Question
JAVA 1.1) Using a text editor, copy the data below into a text file called ziptable.txt. Each line of the table contains a state abbreviation,
JAVA
1.1) Using a text editor, copy the data below into a text file called ziptable.txt. 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 or 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.
As the first step, write a program that reads and prints each line in the table exactly as it appears in the file.
Here is the table data:
AL Alabama 350-369
AK Alaska 995-999
AS American Samoa 967-967
AZ Arizona 850-865
AR Arkansas 716-729 755
CA California 900-966
CO Colorado 800-816
CT Connecticut 060-069
DE Delaware 197-199
DC District of Columbia 200-205
FM FS of Micronesia 969-969
FL Florida 320-349
GA Georgia 300-319 398-399
GU Guam 969-969
HI Hawaii 967-968
ID Idaho 832-838
IL Illinois 600-629
IN Indiana 460-479
IA Iowa 500-528
KS Kansas 660-679
KY Kentucky 400-427
LA Louisiana 700-714
ME Maine 039-049
MH Marshall Islands 969-969
MD Maryland 206-219
MA Massachusetts 010-027 055
MI Michigan 480-499
MN Minnesota 550-567
MS Mississippi 386-397
MO Missouri 630-658
MT Montana 590-599
NE Nebraska 680-693
NV Nevada 889-898
NH New Hampshire 030-039
NJ New Jersey 070-089
NM New Mexico 870-884
NY New York 005 063 090-149
NC North Carolina 269-289
ND North Dakota 580-588
MP N. Mariana Islands 969-969
OH Ohio 430-459
OK Oklahoma 730-749
OR Oregon 970-979
PW Palau Island 969-969
PA Pennsylvania 150-196
PR Puerto Rico 006-009
RI Rhode Island 028-029
SC South Carolina 290-299
SD South Dakota 570-577
TN Tennessee 370-385
TX Texas 750-799 885
UT Utah 840-847
VT Vermont 050-059
VA Virginia 201 220-246
VI Virgin Islands 008-008
WA Washington 980-994
WI Wisconsin 530-549
WV West Virginia 247-268
WY Wyoming 820-831
1.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. 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 Yorks ZIP code of 005 should be printed as 005-005. There are a couple of hurdles to completing this:
1) Some states like New Jersey have multi-part names, and
2) We need to determine which strings are ZIP code specifiers.
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 - 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.
1.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. Use a text editor to examine the output file and verify that it is correct.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ive done 1.1 which reads the list. You dont have to use it.
import java.io.*; import java.util.*; //import java.util.Scanner;
import java.util.*; public class lab {
public static void main(String[] args) { //CREATE FILE w/ catch error mssg // final Formatter x; // // try{ // x = new Formatter("ziptable.txt"); // System.out.println("File was created"); // } // catch(Exception e){ // System.out.println("Error in file creation"); // } //CREATE FILE w/ catch error mssg //Calls all our methods located in read.java read in = new read(); in.openFile(); in.readFile(); in.closeFile(); }
}
public class read { //create a scanner private Scanner file; public void openFile(){ //Try to open file and set it equal to File try{ file= new Scanner(new File("ziptable.txt")); } //Catch exception to make sure file opened without error. Print message if error catch(Exception e){ System.out.println("Could not find File"); } } // Method using boolean while loop to read the whole list and print it if theres a next line available public void readFile(){ while(file.hasNextLine()) { System.out.println(file.nextLine()); } } //method to close it public void closeFile(){ file.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started