Hi I'm super lost as to how you would compare three things. The textbook example is:
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)
System.out.println("The cities in alphabetical order are:");
System.out.println(city1);
System.out.println(city2);
else
System.out.println("The cities in alphabetical order are:");
System.out.println(city2);
System.out.println(city1);
So how would you compare a third and alphabetize?
You can sort alphabetical, the term is lexicographical, by using built in sorting algorithms (or your own). For example by using Collections#sort
(documentation). Note that String
s are comparable and use the lexicographical order by default. That is why you do not need to explicitly specify the order, for example by using a Comparator
object.
This snippet sorts the cities and prints them:
List<String> cities = Arrays.asList({city1, city2, city3});
Collections.sort(cities);
System.out.println("Cities sorted lexicographical:");
for (String city : cities) {
System.out.println(city);
}
Or if you prefer a compact Java 8 solution using Streams
(it essentially falls back to the same methods, especially the same sorting method):
Stream.of(city1, city2, city3).sorted().forEach(System.out::println);
Note that the String#compareTo
method also compares for lexicographical order, as said before. So instead of using a sorting algorithm (which checks the results of compareTo
in a clever way) you can also directly hardcode the comparisons (like you already tried):
String smallestCity;
if (city1.compareTo(city2) < 0 && city1.compareTo(city3) < 0) {
smallestCity = city1;
} else if (city2.compareTo(city1) < 0 && city2.compareTo(city3) < 0) {
smallestCity = city2;
} else if (city3.compareTo(city1) < 0 && city3.compareTo(city2) < 0) {
smallestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String biggestCity;
if (city1.compareTo(city2) > 0 && city1.compareTo(city3) > 0) {
biggestCity = city1;
} else if (city2.compareTo(city1) > 0 && city2.compareTo(city3) > 0) {
biggestCity = city2;
} else if (city3.compareTo(city1) > 0 && city3.compareTo(city2) > 0) {
biggestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String middleCity;
if (city1.compareTo(smallestCity) > 0 && city1.compareTo(biggestCity) < 0) {
middleCity = city1;
} else if (city2.compareTo(smallestCity) > 0 && city2.compareTo(biggestCity) < 0) {
middleCity = city2;
} else if (city3.compareTo(smallestCity) > 0 && city3.compareTo(biggestCity) < 0) {
middleCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
The method String#compareTo
returns 0
if the elements are equal, < 0
if the first element is smaller and > 0
if greater than the second element (documentation).
But as said, a sorting algorithm performs these checks in a far more clever way, with less comparisons. So you should use one.