Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java . IDE: eclipse . Strings Write a Java program that does the following: 1. A

ID: 3729610 • Letter: J

Question

Java . IDE: eclipse . Strings

Write a Java program that does the following: 1. Asks the user for the names of 3 cities, separated by spaces. It is assumed that a given city name won't have spaces in t. So assume you won't have a city name like "Santa Monica". For example your program prints this: "Please input 3 city names separated by spaces in one line and press Enter." r then will type something like: Glendale Pasadena Burbank and press Enter. 2. Your program then prints the 3 city names in alphabetical order. For example: Burbank Glendale Pasadena The goal of this assignment is to get used to Strings in Java and how to compare those strings. Look for a string method that helps you compare two strings and use it's output to compare the 3 city names above. You may also need to use the if/else statement you already learned about and maybe even the switch statement if you like to accomplish this assignment.

Explanation / Answer

The following java programe discribes coding for 3 cities.

CODE:

Create a file named app.java and paste given code. Filename must be app.java

import java.util.Scanner;

public class app {

public static void main(String[] args)

{

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the city names");

String line = scanner.nextLine();

String [] cities = line.split("\s");

String s1 = cities[0]; String s2 = cities[1]; String s3 = cities[2];

if(s1.compareTo(s2) < 0)

{

if( s1.compareTo(s3) >= 0 )

{

System.out.println(s3 + " "+ s1 + " "+ s2);

}

else if(s1.compareTo(s3) < 0 && s2.compareTo(s3) >= 0)

{

System.out.println(s1 + " "+ s3 + " "+ s2);  

}

else

{

System.out.println(s1 + " "+ s2 + " "+ s3);  

}

}

else if(s1.compareTo(s2) > 0)

{

if( s2.compareTo(s3) >= 0 )

{

System.out.println(s3 + " "+ s2 + " "+ s1);

}

else if(s2.compareTo(s3) < 0 && s1.compareTo(s3) >= 0)

{

System.out.println(s2 + " "+ s3 + " "+ s1);  

}

else

{

System.out.println(s2 + " "+ s1 + " "+ s3);  

}  

}

else

{

if( s1.compareTo(s3) >= 0 )

{

System.out.println(s3 + " "+ s1 + " "+ s2);

}

else

{

System.out.println( s1 + " "+ s2 + " " + s3);  

}

}  

}

}

Sample Input:

Delhi Bombay Canc

Sample Output:

Bombay Canc Delhi

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote