Order three cities Write a program that prompts the user to

(Order three cities) Write a program that prompts the user to enter three cities and displays them in ascending order. Here is a sample run:

Solution

CityDemo.java

import java.io.BufferedReader;//package for buffered reader
import java.io.IOException;//package for ioexception
import java.io.InputStreamReader;

public class CityDemo {//main class

public static void main(String[] args) throws IOException{//main method

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\"Enter the first city: \");
String City1 =br.readLine();//keyboard inputting
System.out.print(\"Enter the second city: \");
String City2 = br.readLine();//keyboard inputting
System.out.print(\"Enter the third city: \");
String City3 = br.readLine();//keyboard inputting
br.close();


String temp;
if (City1.compareTo(City2) > 0) {//comparing strings
temp = City1;
City1 = City2;
City2 = temp;
}
if (City2.compareTo(City3) > 0) {//comparing strings
temp = City2;
City2 = City3;
City3 = temp;
}
if (City1.compareTo(City2) > 0) {//comparing strings
temp = City1;
City1 = City2;
City2 = temp;
}

System.out.println(\"The three cities in alphabetical order are: \");
System.out.println(City1);
System.out.println(City2);
System.out.println(City3);

}
}

output

Enter the first city: america
Enter the second city: canada
Enter the third city: algeria
The three cities in alphabetical order are:
algeria
america
canada

(Order three cities) Write a program that prompts the user to enter three cities and displays them in ascending order. Here is a sample run:SolutionCityDemo.jav

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site