// Capitals.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2002-11-30 File created. 2016-09-25 Last modification. import java.util.* ; class Capitals { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; String countries_and_capitals = "Finland Helsinki Usa Washington Denmark Copenhagen " + "Afghanistan Kabul Russia Moscow England London " + "Italy Rome France Paris Spain Madrid " + "Portugal Lisbon Chile Santiago Japan Tokyo " + "Sweden Stockholm Norway Oslo Pakistan Islamabad " + "Iceland Reykjavik Hungary Budapest Holland Amsterdam " + "Belgium Brussels Austria Vienna 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 ) ; int index_of_country_name = countries_and_capitals. indexOf( country_name ) ; if ( index_of_country_name != -1 ) { // The country name was found in countries_and_capitals String text_after_country_name = countries_and_capitals. substring( index_of_country_name + country_name.length() ) ; // A space at the end of the string ensures that also // the last country-capital pair in the string works. text_after_country_name = text_after_country_name.trim() + " " ; String capital_name = text_after_country_name.substring( 0, text_after_country_name.indexOf( " " ) ) ; System.out.print( "\n The capital of " + country_name + " is " + capital_name + ".\n\n" ) ; } } }