Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Problem: This challenge is intended to give you some additional practice in working with lists and maps in Java. We have a database of

Java Problem:

This challenge is intended to give you some additional practice in working with lists and maps in Java.

We have a "database" of names and addresses in comma separated values (CSV) format. This format is often used to store tables of information in text form. In our case, the "rows" (also known as records) correspond to information about a single person, and the "columns" (also known as fields) correspond to individual attributes of the person. The fields are joined with commas, which act as delimiters for the fields. The first row of the database, called the header row, contains descriptive labels for the fields. Each of the subsequent rows corresponds to the information of a person.

A skeleton of a Java program is provided for you as a demonstration of one way to store a CSV table in memory. The program as written performs the following operations:

Create the database as an ArrayList of HasMap.

Create a Scanner associated with the keyboard

Read the header row and split it (using the comma as the delimiter) into an ArrayList called "keys" to get the field names ("FirstName", "MiddleInitial", etc).

For each of the subsequent rows, the program will do:

Read and split the row into an ArrayList called "values" so that the elements of the list are the fields of the record.

Pair the previous fields (ArrayList called "values") with the corresponding field names from the header row (ArrayList called "keys"), so the resulting pairs then become the key/value pairs in a new HashMap for the row.

Add the resulting HashMap to the database (ArrayList called "database"), which acts as a "container" for all the records in the CSV table. This nested data structure, a list of maps, makes it easy to retrieve any desired field for any of the keys ("FirstName", "MiddleInitial", etc), while preserving the original order of the records as given in the CSV table.

Finally, close the Scanner associated with the keyboard.

The program skeleton includes documentation in the form of comments. Your task is to use this program as a starting point, and add instructions to print the information in the collection in the form of standard address blocks. You must insert code only in the specified part.

Input Format

A CVS table of seven fields. The first row (header row) is the name of the fields. The subsequent rows are real records of the database, that is, they contain name and address information of people.

Output Format

For each record, the output will consist of 3 lines, as shown in the test case. The information of a person must be separetaed by a blank line from the information of the next person. Note that there is no blank line after the information of the last person.

Sample Input 0

FirstName,MiddleInitial,LastName,StreetAddress,City,State,ZipCode Martha,C,Zermeno,928 Arrowood Drive,Jacksonville,FL,32217 Erin,F,Smallwood,4474 Elk Street,Los Angeles,CA,90017 Dewayne,J,Brown,2403 Hurry Street,Elkton,VA,22827 Latrisha,G,Thornhill,1422 Oliver Street,Fort Worth,TX,76118 Larue,J,Villarreal,2907 Joy Lane,Calabasas,CA,91302 

Sample Output 0

Martha C Zermeno 928 Arrowood Drive Jacksonville, FL 32217 Erin F Smallwood 4474 Elk Street Los Angeles, CA 90017 Dewayne J Brown 2403 Hurry Street Elkton, VA 22827 Latrisha G Thornhill 1422 Oliver Street Fort Worth, TX 76118 Larue J Villarreal 2907 Joy Lane Calabasas, CA 91302 

Code:

import java.util.Scanner; import java.util.ArrayList; import java.util.HashMap; import java.util.Arrays;

public class Solution {

public static void main(String[] args) {

/* Container for the whole database */ // Create an ArrayList to hold objects of type HashMap. // Each subsequent row of the database will be store in a HashMap. ArrayList> database = new ArrayList<>();

Scanner in = new Scanner(System.in);

/* Get list of keys from the header row; split into an ArrayList */ String header = in.nextLine(); // Holds key names from header row ("FirstName", "MiddleInitial", etc) ArrayList keys = new ArrayList(Arrays.asList(header.split(",")));

/* Read and process every subsequent row (record) in CSV table */ while ( in.hasNextLine() ) {

/* Get next line in the CSV table (subsequent rows) */ String line = in.nextLine(); /* Split comma-separated values into ArrayList called "values" */ ArrayList values = new ArrayList(Arrays.asList(line.split(",")));

/* Create new map for current row (subsequent rows) */ // Holds the key/value pairs for each subsequent row HashMap row = new HashMap(); /* Populate map, using key names from the header row */ for (int i = 0; i < keys.size(); i++) row.put(keys.get(i), values.get(i));

/* Add map to database container */ database.add(row); }

/* Close the Scanner */ in.close();

/* Loop through the database; print as formatted address blocks */ /* INSERT YOUR CODE HERE */

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Icdt 88 2nd International Conference On Database Theory Bruges Belgium August 31 September 2 1988 Proceedings Lncs 326

Authors: Marc Gyssens ,Jan Paredaens ,Dirk Van Gucht

1st Edition

3540501711, 978-3540501718

More Books

Students also viewed these Databases questions