/* CapitalsMap.java (c) Kari Laitinen http://www.naturalprogramming.com 2016-09-23 File created. This is a modified version of program Capitals.java In this version data is stored in a data structure called HashMap. */ import java.util.* ; class CapitalsMap { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; // The following HashMap will contain Key-Value pairs // which are both String objects. It will be very easy // to search for a Value with a Key. HashMap countries_and_capitals = new HashMap() ; countries_and_capitals.put( "Finland", "Helsinki" ) ; countries_and_capitals.put( "Usa", "Washington" ) ; countries_and_capitals.put( "Denmark", "Copenhagen" ) ; countries_and_capitals.put( "Afghanistan", "Kabul" ) ; countries_and_capitals.put( "Russia", "Moscow" ) ; countries_and_capitals.put( "England", "London" ) ; countries_and_capitals.put( "Italy", "Rome" ) ; countries_and_capitals.put( "France", "Paris" ) ; countries_and_capitals.put( "Spain", "Madrid" ) ; countries_and_capitals.put( "Portugal", "Lisbon" ) ; countries_and_capitals.put( "Chile", "Santiago" ) ; countries_and_capitals.put( "Japan", "Tokyo" ) ; countries_and_capitals.put( "Sweden", "Stockholm" ) ; countries_and_capitals.put( "Norway", "Oslo" ) ; countries_and_capitals.put( "Pakistan", "Islamabad" ) ; countries_and_capitals.put( "Iceland", "Reykjavik" ) ; countries_and_capitals.put( "Hungary", "Budapest" ) ; countries_and_capitals.put( "Holland", "Amsterdam" ) ; countries_and_capitals.put( "Belgium", "Brussels" ) ; countries_and_capitals.put( "Austria", "Vienna" ) ; countries_and_capitals.put( "Israel", "Jerusalem" ) ; System.out.print("\n This program may know the capital of a country." + "\n Type in the name of some country: " ) ; String country_name = keyboard.nextLine().toLowerCase() ; country_name = country_name.substring( 0, 1 ).toUpperCase() + country_name.substring( 1 ) ; if ( countries_and_capitals.containsKey( country_name ) ) { // The country name was found in countries_and_capitals String capital_name = countries_and_capitals.get( country_name ) ; System.out.print( "\n The capital of " + country_name + " is " + capital_name + ".\n\n" ) ; } else { System.out.print( "\n Sorry, I do not know the capital of " + country_name + ".\n\n" ) ; } } }