Java String compare two String values read from console

Question

We would like to prompts the user to enter two cities and displays them in alphabetical order.


import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      //your code here;
   }/*  ww  w.  j a v  a  2s.  c  o m*/
}



import java.util.Scanner;

public class Main {
   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);

      input.close();
   }
}



PreviousNext

Related