Write a .java program Read the state2presidents.txt file into a map such that wh
ID: 3574251 • Letter: W
Question
Write a .java program
Read the state2presidents.txt file into a map such that when you print the map out the rows are sorted vertically by state and each row's set of presidents on that row are sorted alphabetically
state2presidents.txt
This first version of output for STEP#1 using the toString of the map is only worth 60% of the 70%
(This all one string on a single line. Your output may wrap/line-break differently on your display based on font/window size)
{Arkansas=[Clinton], California=[Nixon], Connecticut=[Bush_GW], Georgia=[Carter], Hawaii=[Obama], Illinois=[Reagan], Iowa=[Hoover], Kentucky=[Lincoln], Massachusetts=[Bush_GHW, Kennedy], Missouri=[Truman], Nebraska=[Ford], New_Hampshire=[Pierce], New_Jersey=[Cleveland], New_York=[Fillmore, Roosevelt_F, Roosevelt_T, VanBuren], North_Carolina=[Johnson_A, Polk] , Ohio=[Garfield, Grant, Harding, Harrison_B, Hayes, McKinley, Taft], Pennsylvania=[Buchanan], Texas=[Eisenhower, Johnson_L], Vermont=[Arthur, Coolidge], Virginia=[Taylor, Tyler, Wilson]}
This second version of output for STEP #1 is required to get the full 70%
(This output is explicitly broken by line breaks and must match EXACTLY)
Arkansas Clinton
California Nixon
Connecticut Bush_GW
Georgia Carter
Hawaii Obama
Illinois Reagan
Iowa Hoover
Kentucky Lincoln
Massachusetts Bush_GHW Kennedy
Missouri Truman
Nebraska Ford
New_Hampshire Pierce
New_Jersey Cleveland
New_York Fillmore Roosevelt_F Roosevelt_T VanBuren
North_Carolina Johnson_A Polk
Ohio Garfield Grant Harding Harrison_B Hayes McKinley Taft
Pennsylvania Buchanan
Texas Eisenhower Johnson_L
Vermont Arthur Coolidge
Virginia Taylor Tyler Wilson
Explanation / Answer
ANSWER:
package com.cg.cangg.readfrom.fileand.sort;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class MapSort {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(
"resource/state2presidents.txt"));
TreeMap<String, String[]> map = new TreeMap<String, String[]>();
while (scanner.hasNextLine()) {
String[] columns1 = scanner.nextLine().split(" ");
int length = columns1.length;
String[] columns2 = new String[length - 1];
for (int i = 1; i < length; i++) {
columns2[i - 1] = columns1[i];
}
Arrays.sort(columns2);
map.put(columns1[0], columns2);
}
for (Entry<String, String[]> entry : map.entrySet()) {
String city = entry.getKey();
String[] streets = entry.getValue();
System.out.print(city);
for (String temp : streets) {
System.out.print(" " + temp);
}
System.out.println();
}
scanner.close();
}
}
Explanation:
1) Give the .txt file path correctly
2) Here I'm declare a TreeMap<String, String[]>, so that first values
i.e, States are sorted while adding into TreeMap
3) And the remaining streets are stored into String of array,
this array is also sorted using Arrays.sort() method
4) After that add these two(String, ArrayOfString) into TreeMap
5) Print the TreeMap after all elements added by using forEach loop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.