Programming help!!! Fast! orderthreecities

Programming
Tutor: None Selected Time limit: 1 Day

Write

a program (ThreeCities.java) that
prompts the user to enter three cities and displays them in ascending order.

Here
is a sample run:



  


 

Enter the first city: Chicago


 

Enter the second city: Los
  Angeles


 

Enter the third city: Atlanta


 

The three cities in alphabetical order are Atlanta, Chicago, and Los
  Angeles.

Jul 6th, 2015

Thank you for the opportunity to help you with your question!

Do you have Eclipse programme ??? 


Please let me know if you need any clarification. I'm always happy to answer your questions.
Jul 6th, 2015

yes I do! so far I have this but Im stuck, can you help??


import java.util.Scanner;

public class OrderTwoCities {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    // Prompt the user to enter two cities
    System.out.print("Enter the first city: ");
    String city1 = input.nextLine();
    System.out.print("Enter the second city: ");
    String city2 = input.nextLine();
    
    if (city1.compareTo(city2) < 0)
      System.out.println("The cities in alphabetical order are " +
         city1 + " " + city2);
    else
      System.out.println("The cities in alphabetical order are " +
          city2 + " " + city1);
  }
}

I know how to complete two, but with three cities Im stuck


Jul 7th, 2015

OK just wait , i will give an other version best than your program

Jul 7th, 2015

Thank you very much! My instructor told me that I may consider using the string method compareTo(), if-else if statement, or switch statement. I just have trouble using these methods. 

Jul 7th, 2015

do you know the Loop "for();"  ?????

Jul 7th, 2015

Not really, no. We just started discussing all of the in depth coding recently. I'm just not sure where to go from here on my code, I've tried adding this statement below, but it would not run on eclipse :/ Is it as simple as adding more content? Or do I change everything?

System.out.print("Enter the third city: ");
    String city3 = input.nextLine();

Jul 7th, 2015

don t worry i will make it run for you ASAP 

Jul 7th, 2015

Thank you so much!

Jul 7th, 2015

I saw you also could help me with my other question! Thank you again for this. Have you figured out what I did wrong in this question?

Jul 7th, 2015

yes i am working now for it , just i have a trouble on my computer is very low , 

and i tacked 30 min , to eat dinner and i am back just now 

yes i also able to help you on your other exercise 

Jul 7th, 2015

Thats great! I appreciate it. 

Jul 7th, 2015

Sorry for late but this program need time 

Jul 7th, 2015

if i use the Loop " for"   and array i will make very fast 

Jul 7th, 2015

That's okay. How long do you think it takes? Is it easier to do with the loop "for" and arrays?

Jul 7th, 2015

lesson6hw.pdf  This is what she gave us. I've completed everything except the working program

Jul 7th, 2015

Im sorry thats the wrong one for this question. This is the correct PDF lesson6hwp2.pdf 

Jul 7th, 2015

this is the programme try it on your computer : 


package ThreeCities;

import java.util.Scanner;

public class ThreeCities {

public static void main(String[] argv){ 

Scanner input = new Scanner(System.in);

 

System.out.println("Enter the first city : ");

String city1 = input.nextLine();

System.out.println("Enter the second city : ");

String city2 = input.nextLine();

System.out.println("Enter the third city : ");

String city3 = input.nextLine();

if((city1.compareTo(city2)<0) && (city2.compareTo(city3)<0))

       {

System.out.println("The three cities in alphabetical order are : " +city1+""+city2+""+city3);

       }

       

       if((city2.compareTo(city1)<0)&&(city1.compareTo(city3)<0))

       {

        System.out.println("The three cities in alphabetical order are : " +city2+""+city1+""+city3);

       }

       //Compare

       if((city3.compareTo(city2)<0)&&(city2.compareTo(city1)<0))

       {

       System.out.println("The three cities in alphabetical order are : " +city3+""+city2+""+city1);

       }

}

}


Jul 7th, 2015

It worked half way, I just couldnt get the last statement to appear where is states to the user "The three cities in alphabetical order are ....." 

Jul 7th, 2015

ahh the problem just on the concatination of the variable , i will make it run 

Jul 7th, 2015

Great!

Jul 7th, 2015

try this it work for me ver well : 


package ThreeCities;

import java.util.Scanner;

public class ThreeCities {

public static void main(String[] argv){ 

Scanner input = new Scanner(System.in);

 

System.out.println("Enter the first city : ");

String city1 = input.nextLine();

System.out.println("Enter the second city : ");

String city2 = input.nextLine();

System.out.println("Enter the third city : ");

String city3 = input.nextLine();

if((city1.compareTo(city2)<0) && (city2.compareTo(city3)<0))

       {

System.out.println("The three cities in alphabetical order are : " + city1 +" "+ city2 +" "+ city3);

       }

       

       if((city2.compareTo(city1)<0) && (city1.compareTo(city3)<0))

       {

        System.out.println("The three cities in alphabetical order are : " +city2+" "+city1+" "+city3);

       }

       

       if((city3.compareTo(city2)<0) && (city2.compareTo(city1)<0))

       {

       System.out.println("The three cities in alphabetical order are : " +city3+" "+city2+" "+city1);

       }

}

}


Jul 7th, 2015

tack a look : 

Sans sds.png 

Jul 7th, 2015

Hm, thats intersting. It still is not working for me. Can you send the code again please? also with a full snapshot of the code? 

Jul 7th, 2015

package ThreeCities;

import java.util.Scanner;

public class ThreeCities {

public static void main(String[] argv){ 

Scanner input = new Scanner(System.in);

System.out.print("Enter the first city : ");

String city1 = input.nextLine();

System.out.print("Enter the second city : ");

String city2 = input.nextLine();

System.out.print("Enter the third city : ");

String city3 = input.nextLine();





   if((city1.compareTo(city2)<0) && (city2.compareTo(city3)<0))

       {System.out.println("The three cities in alphabetical order are : " +city1+" "+city2+" "+city3);}

   else if((city1.compareTo(city3)<0) && (city3.compareTo(city2)<0))

            {System.out.println("The three cities in alphabetical order are : " +city1+" "+city3+" "+city2);}

   

   else if((city2.compareTo(city1)<0) && (city1.compareTo(city3)<0))

       {System.out.println("The three cities in alphabetical order are : " +city2+" "+city1+" "+city3);}

   else if((city2.compareTo(city3)<0) && (city3.compareTo(city1)<0))

            {System.out.println("The three cities in alphabetical order are : " +city2+" "+city3+" "+city1);}

   

   

   else if((city3.compareTo(city2)<0) && (city2.compareTo(city1)<0))

       {System.out.println("The three cities in alphabetical order are : " +city3+" "+city2+" "+city1);}

   else if((city3.compareTo(city1)<0) && (city1.compareTo(city2)<0))

            {System.out.println("The three cities in alphabetical order are : " +city3+" "+city1+" "+city2);}

   else 

   System.out.println("Error");




}

}


Jul 7th, 2015

Are you studying on the go? Check out our FREE app and post questions on the fly!
Download on the
App Store
...
Jul 6th, 2015
...
Jul 6th, 2015
Dec 5th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer