/* this program will input 3 city names and determine their proper ordering using nested if-else logic */ import java.util.*; // for the Scanner public class Prog4_24 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // create a Scanner for input String city1, city2, city3; // the 3 input cities System.out.println("Enter three city names, pressing enter after each. If the city contains blank spaces, replace the spaces with _ characters and remove any periods as in St_Louis"); city1 = in.next(); // input the 3 cities (note: only using 1 prompt) city2 = in.next(); city3 = in.next(); if(city1.compareTo(city2)<0) // city1 < city2, now compare city2 and city3 if(city2.compareTo(city3)<0) // city1 < city2 < city3 System.out.println("the three cities in alphabetical order are " + city1 + ", " + city2 + ", " + city3); else if(city1.compareTo(city3)<0) // need to compare city1 to city3 System.out.println("the three cities in alphabetical order are " + city1 + ", " + city3 + ", " + city2); // city1 < city3 < city2 else System.out.println("the three cities in alphabetical order are " + city3 + ", " + city1 + ", " + city2); // city3 < city1 < city2 else if(city3.compareTo(city2)<0) // if we reach here, we know city2 < city1, now test if city3 < city2 giving us System.out.println("the three cities in alphabetical order are " + city3 + ", " + city2 + ", " + city1); // city3 < city2 < city1 else if(city3.compareTo(city1)<0) // otherwise we have city2 < city1 and need to compare city1 to city3 System.out.println("the three cities in alphabetical order are " + city2 + ", " + city3 + ", " + city1); // city2 < city3 < city1 else System.out.println("the three cities in alphabetical order are " + city2 + ", " + city1 + ", " + city3); // city2 < city1 < city3 } }