Hello I need to add lines to the following code to get an output like the one at the bottom: JAVA File: import java.util
Posted: Sat May 14, 2022 2:35 pm
Hello I need to add lines to the following code to get an output
like the one at the bottom:
JAVA File:
import java.util.*;
import java.io.*;
public class Capitals
{
public static void main(String[] args)
{
HashMap capNStates = readData("States_Capital.csv");
ArrayList states = new
ArrayList<>(capNStates.keySet());
String state = states.remove(10);
System.out.println( capNStates.get(state) );
}
public static HashMap readData( String filename )
{
//exception handling
try
{
HashMap data = new HashMap<>();
File f = new File( filename );
Scanner scan = new Scanner(f);
while( scan.hasNextLine () )
{
String[] line = scan.nextLine.split(",");
data.put( line[0], line[1] );
}
return data;
}catch(FileNotFoundException e)
{
System.out.println( filename + " not found ");
return null;
}
}
}
This is how the output should look like:
Since I cannot provide the CSV File, here's a picture of what it
looks like:
Score: 0 State: delaware Capital: edvro dover Score: 5 State: new york Capital: lbnyaa albany Score: 10 State: west virginia Capital: nroehtalsc rochester Score: 8 State: West Virginia Capital: norcteslah quit
A B 1 Alabama Montgomery 2 Alaska Juneau 3 Arizona Phoenix 4 Arkansas Little Rock 5 California Sacramento 6 Colorado Denver Connecticut Hartford 7 8 9 8 Delaware Dover 9 Florida Tallahassee 10 Georgia Atlanta 11 Hawaii Honolulu 12 Idaho 13 Illinois 14 Indiana Boise Springfield Indianapolis Des Moines Topeka 15 lowa 16 Kansas 17 Frankfort Kentucky Louisiana 18 Baton Rouge 19 Maine Augusta Annapolis 20 Maryland 21 Massachusetts Boston 22 Michigan Lansing Saint Paul 23 Minnesota 24 Mississippi Jackson 25 Missouri Jefferson City
like the one at the bottom:
JAVA File:
import java.util.*;
import java.io.*;
public class Capitals
{
public static void main(String[] args)
{
HashMap capNStates = readData("States_Capital.csv");
ArrayList states = new
ArrayList<>(capNStates.keySet());
String state = states.remove(10);
System.out.println( capNStates.get(state) );
}
public static HashMap readData( String filename )
{
//exception handling
try
{
HashMap data = new HashMap<>();
File f = new File( filename );
Scanner scan = new Scanner(f);
while( scan.hasNextLine () )
{
String[] line = scan.nextLine.split(",");
data.put( line[0], line[1] );
}
return data;
}catch(FileNotFoundException e)
{
System.out.println( filename + " not found ");
return null;
}
}
}
This is how the output should look like:
Since I cannot provide the CSV File, here's a picture of what it
looks like:
Score: 0 State: delaware Capital: edvro dover Score: 5 State: new york Capital: lbnyaa albany Score: 10 State: west virginia Capital: nroehtalsc rochester Score: 8 State: West Virginia Capital: norcteslah quit
A B 1 Alabama Montgomery 2 Alaska Juneau 3 Arizona Phoenix 4 Arkansas Little Rock 5 California Sacramento 6 Colorado Denver Connecticut Hartford 7 8 9 8 Delaware Dover 9 Florida Tallahassee 10 Georgia Atlanta 11 Hawaii Honolulu 12 Idaho 13 Illinois 14 Indiana Boise Springfield Indianapolis Des Moines Topeka 15 lowa 16 Kansas 17 Frankfort Kentucky Louisiana 18 Baton Rouge 19 Maine Augusta Annapolis 20 Maryland 21 Massachusetts Boston 22 Michigan Lansing Saint Paul 23 Minnesota 24 Mississippi Jackson 25 Missouri Jefferson City