import java.io.*; import java.util.*; public class HashMapDemo1 { public static void main( String[] args ) throws Exception { BufferedReader States2CapitolsFile = new BufferedReader( new FileReader("States2Capitols.txt") ); HashMap States2CapitolsMap = new HashMap(); while ( States2CapitolsFile.ready() ) { String line = States2CapitolsFile.readLine(); // i.e. "Ohio Columbus" String[] tokens = line.split("\\s+"); // split around " " produces an array of 2 strings String state = tokens[0], capitol = tokens[1]; // tokens = ["Ohio"]["Columbus"] States2CapitolsMap.put( state, capitol ); // hash value of state determines which row of the map, the pair goes into } System.out.println("Here is a list of State -> Capitol pairs:"); for ( String state : States2CapitolsMap.keySet() ) System.out.println( state + " -> " + States2CapitolsMap.get( state ) ); // .get(state) returns the capitol System.out.println(); BufferedReader queries = new BufferedReader(new FileReader("queries.txt")); while ( queries.ready() ) { String key = queries.readLine(); // i.e "Ontario" or "Florida" if ( States2CapitolsMap.containsKey( key ) ) System.out.println( "capitol of " + key + " is " + States2CapitolsMap.get(key) ); else System.out.println( key + " is not a state" ); } } // END MAIN } // END CLASS