Write a Java file that STEP #1 worth 70% Read the state2presidents.txt file into
ID: 3574255 • Letter: W
Question
Write a Java file that
STEP #1 worth 70%
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
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
STEP #2 worth 15%
Print the inverse of the state2presidents map such that you list each president on his own line with the state where he was born. You do not have to explicitly store this inversion into a map but you must generate the following output exactly. The lines are sorted by president alphabetically. You may Use the allPresidents.txt file in any way needed for this step but be aware the allPresidents.txt file contains all the presidents of the US - and some of them do not have a birth state associated with them since they were not born before the states were formed. You are only to list those presidents who have a birth state.
Arthur Vermont
Buchanan Pennsylvania
Bush_GHW Massachusetts
Bush_GW Connecticut
Carter Georgia
Cleveland New_Jersey
Clinton Arkansas
Coolidge Vermont
Eisenhower Texas
Fillmore New_York
Ford Nebraska
Garfield Ohio
Grant Ohio
Harding Ohio
Harrison_B Ohio
Hayes Ohio
Hoover Iowa
Johnson_A North_Carolina
Johnson_L Texas
Kennedy Massachusetts
Lincoln Kentucky
McKinley Ohio
Nixon California
Obama Hawaii
Pierce New_Hampshire
Polk North_Carolina
Reagan Illinois
Roosevelt_F New_York
Roosevelt_T New_York
Taft Ohio
Taylor Virginia
Truman Missouri
Tyler Virginia
VanBuren New_York
Wilson Virginia
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sorting;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author abhisheka
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String line,str1,str2,str3;String[] words = null;
InputStream fis = new FileInputStream("C:/Users/abhisheka/Documents/NetBeansProjects/sorting/src/sorting/file.txt");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
//char[] arr;
while ((line = br.readLine()) != null) {
line= line.replaceAll("\[(.*?)\]", "");
line = line.replaceAll(",", "");
line = line.replaceAll("=", "");
//line = line.replaceAll("{","");
// line = line.replaceAll("}","");
words = line.split(" ");
}
int h=words.length;
for (int i = 0; i<h;i++){
System.out.println(words[i]);
}
//
}
public static void sortStringBubble( String x [ ] )
{
int j;
boolean flag = true; // will determine when the sort is finished
String temp;
while ( flag )
{
flag = false;
for ( j = 0; j < x.length - 1; j++ )
{
if ( x [ j ].compareToIgnoreCase( x [ j+1 ] ) > 0 )
{ // ascending sort
temp = x [ j ];
x [ j ] = x [ j+1]; // swapping
x [ j+1] = temp;
flag = true;
}
}
}
}
}
using regex parse all string in array then use bubble sort to genereate list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.