Question

Write a java project that reads a sequence of up to 25 pairs of names and...

Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each line of input will contain strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate format to the screen. Need to Store the data in an ArrayList object. DATA SHOULD BE READ FROM INPUT FILE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Record.java

public class Record {
private String firstName, lastName;
private int postalCode;
private String streetAddress, city, state;
private long phoneNumber;
  
public Record()
{
this.firstName = "";
this.lastName = "";
this.postalCode = 0;
this.streetAddress = "";
this.city = "";
this.state = "";
this.phoneNumber = 0;
}

public Record(String firstName, String lastName, int postalCode, String streetAddress,
String city, String state, long phoneNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.postalCode = postalCode;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
this.phoneNumber = phoneNumber;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public int getPostalCode() {
return postalCode;
}

public String getStreetAddress() {
return streetAddress;
}

public String getCity() {
return city;
}

public String getState() {
return state;
}

public long getPhoneNumber() {
return phoneNumber;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setPostalCode(int postalCode) {
this.postalCode = postalCode;
}

public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}

public void setCity(String city) {
this.city = city;
}

public void setState(String state) {
this.state = state;
}

public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
}

RecordReader.java (Main class)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class RecordReader {
  
private static final String FILENAME = "records.txt";
  
public static void main(String[] args)
{
Scanner fileReader;
ArrayList<Record> records = new ArrayList<>();
  
try
{
fileReader = new Scanner(new File(FILENAME));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(" ");
String fName = data[0];
String lName = data[1];
int zip = Integer.parseInt(data[2]);
String street = data[3];
String city = data[4];
String state = data[5];
long phone = Long.parseLong(data[6]);
  
records.add(new Record(fName, lName, zip, street, city, state, phone));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("File not found: " + FILENAME);
System.exit(0);
}catch(NumberFormatException nfe){
System.out.println("Unknown character encountered!\nExiting..");
System.exit(0);
}
  
System.out.printf("%15s %20s %30s %15s %15s %15s\n------------------------------------------------"
+ "-------------------------------------------------------------------\n",
"Name", "Postal Code", "Street Address", "City", "State", "Phone Number");
  
for(Record rec : records)
{
System.out.printf("%15s %20d %30s %15s %15s %15d\n",
rec.getFirstName() + " " + rec.getLastName(), rec.getPostalCode(), rec.getStreetAddress(),
rec.getCity(), rec.getState(), rec.getPhoneNumber());
}
}
}

Note: The input file "records.txt" has to be created before starting the program. The file needs to be created within the project directory where the .java files will reside.

******************************************************************** SCREENSHOT *******************************************************

INPUT FILE (records.txt) : Data inside the file may vary, but each field needs to be separated by tabs

CONSOLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a java project that reads a sequence of up to 25 pairs of names and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • IN JAVA 3 ZIPS, What Order?: Write a program named ZipOrder that the reads in three...

    IN JAVA 3 ZIPS, What Order?: Write a program named ZipOrder that the reads in three zip codes from standard input and prints to standard output one of three words: "ASCENDING", "DESCENDING", "UNSORTED". The zip codes are guaranteed to be distinct from each other. In particular: if each zip code read in is greater than to the previous one, "ASCENDING"  is printed. if each zip code read in is less than to the previous one, "DESCENDING" is printed. otherwise, "UNSORTED" is...

  • Your task for this project is to write a parser for a customer form. You need to develop a Java a...

       Your task for this project is to write a parser for a customer form. You need to develop a Java application using Parboiled library. Your program should include a grammar for the parser and display the parse tree with no errors. Remember that your fifth and sixth assignments are very similar to this project and you can benefit from them. The customer form should include the following structure: First name, middle name (optional), last name Street address, city, state (or...

  • C PROGRAMMING: Define a set of data structures to store the information for a large real...

    C PROGRAMMING: Define a set of data structures to store the information for a large real estate agency. The agency can be described with: agency name, headquarters, address (street, city, state, zip), and the records for up to 100 real estate agents located in different cities. Each realtor can be described with the following data: agent name, office address (street, city, state, zip), phone number, and houses for sale (consisting of up to 10 house records, each of which has...

  • In this lab assignment, you'll write code that parses an email address and formats the ci and zip...

    Using Microsoft Visual Studio C# In this lab assignment, you'll write code that parses an email address and formats the ci and zip code portion of an address. String Handling Email: [email protected] Parse City: Fresno State: ca Zp code: 93722 Format ให้ 2 Parsed String Formatted String User name: anne Domain name: murach.comm City, State, Zip: Fresno, CA 93722 OK OK Create a new Windows Forms Application named StringHandling and build the form as shown above. 1. Add code to...

  • please Answer the following regular expressions questions(also do number 9) Q4 Choose the pattern that finds...

    please Answer the following regular expressions questions(also do number 9) Q4 Choose the pattern that finds all filenames in which the first letters of the filename are astA, followed by a digit, followed by the file extension .txt. 1) astA[[:digit:]]\.txt 2) astA[[0-9]].txt 3) astA.\.txt 4) astA[[:digit:]].txt Q5 What's the difference between [0-z]+ and \w+ ? 1) The first one accepts 0 and z and the other doesn't. 2) The first one doesn't allow for uppercase letters. 3) The first one...

  • (JAVA) Using classes and inheritance, design an online address book to keep track of the names

    (JAVA)Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries.Define the following classes: Class Address to store a street name, city, state and zip code. Class Date to store the day, month and year. Class Person to store a person's last name and first name. Class ExtPerson that extends the class...

  • In this assignment, you will write one (1) medium size C program. The program needs to...

    In this assignment, you will write one (1) medium size C program. The program needs to be structured using multiple functions, i.e., you are required to organize your code into distinct logical units. The following set of instructions provide the specific requirements for the program. Make sure to test thoroughly before submitting. Write   a   program,   named   program1.c,   that   reads   and   processes   employee   records   (data   about   an   employee).   Each   employee   record   must   be   stored   using   a   struct   that   contains   the   following  ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT