Write a Java program that does the following: 1. Asks the user for the names of
ID: 3739666 • Letter: W
Question
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 it. 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." The user 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. Look through all the methods that the String class offers you and see which one will be of most use to you? You can read about some of the methods in the book and search through all of them on line in various places. See if you can find the Oracle web site where Java Strings are discussed for the latest version of Java. The more String methods you use the more chance you have of getting extra credit. 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 it. 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." The user 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. Look through all the methods that the String class offers you and see which one will be of most use to you? You can read about some of the methods in the book and search through all of them on line in various places. See if you can find the Oracle web site where Java Strings are discussed for the latest version of Java. The more String methods you use the more chance you have of getting extra credit. 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 it. 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." The user 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. Look through all the methods that the String class offers you and see which one will be of most use to you? You can read about some of the methods in the book and search through all of them on line in various places. See if you can find the Oracle web site where Java Strings are discussed for the latest version of Java. The more String methods you use the more chance you have of getting extra credit.Explanation / Answer
//Coding:
import java.util.*;
class City
{
public static void main (String args[])
{
Scanner s =new Scanner(System.in);
System.out.print("Enter three city names : ");
String str=s.nextLine();
str=str+" ";
int l1=str.length();
String str1="";
String ar[]=new String[3];
int x=0;
for(int i=0;i<l1;i++)
{
char ch=str.charAt(i);
if(ch!=' ')
{
str1=str1+ch;
}
else
{
ar[x]=str1;
str1="";
x=x+1;
}
}
String temp=" ";
for(int j=0;j<ar.length;j++)
{
for(int k=0;k<ar.length-j-1;k++)
{
if(ar[k].compareTo(ar[k+1])>0)
{
temp=ar[k];
ar[k]=ar[k+1];
ar[k+1]=temp;
}
}
}
System.out.print("Result : ");
for(int l=0;l<ar.length;l++)
{
System.out.print(ar[l]+" ");
}
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.