I am a beginner to Java and I have been trying to use the compareTo method in order to sort three cities. My three test words are Legion, LSD and Chunkey. To identify the errors, I have each output possibilities with a number from 1-6. For output 1 it's (Chunkey, LSD, Legion), 2 = (legion, LSD, Chunkey, 4 = (Chunkey, Legion, LSD). The output tends to make no sense when I believe I'm following the right logic that when two strings are compared and the value is a negative value then the first value comes first and then the second value. Yet even doing that, it's not ordered correct. Please help! Thank you!
import java.util.Scanner;
public class OrderingCity {
public static void main (String []args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the First City");
String c1 = input.nextLine();
System.out.println("Enter the Second City");
String c2 = input.nextLine();
System.out.println("Enter the Third City");
String c3 = input.nextLine();
//When the value is less than 0, the first value is first while the second value is second.
//When the first city is compared to the second and if it returns a negative value (<0) then
//the first city comes first and when the value of the second city is compared to the third
//city and it returns a negative value then the second city comes before the third city
if (c1.compareTo(c2) < 0 && c2.compareTo(c3) < 0){
System.out.println(c1 + " 1 " + c2 + " " + c3);
}
//When the first city is compared to the second and it returns a positive value (>0) then
//the second city comes first and when the value of the first city is compared to the third
//city and it returns a positive value (>0) then third city comes before the first.
else if (c1.compareTo(c2) > 0 && c1.compareTo(c3) > 0){
System.out.println(c2 + " 2 " + c3 + " " + c1);
}
else if (c2.compareTo(c3) < 0 && c3.compareTo(c1) < 0){
System.out.println(c2 + " 3 " + c3 + " " + c1);
}
else if (c2.compareTo(c3) > 0 && c2.compareTo(c1) > 0){
System.out.println(c3 + " 4 " + c1 + " " + c2 );
}
else if (c3.compareTo(c1) < 0 && c1.compareTo(c2) < 0){
System.out.println(c3 + " 5 " + c1 + " " + c2);
}
else if (c3.compareTo(c1) > 0 && c3.compareTo(c2) > 0) {
System.out.println(c1 + " 6 " + c2 + " " + c3);
}
}
}
This should help:
import java.util.Scanner;
public class OrderingCity {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the First City");
String c1 = input.nextLine();
System.out.println("Enter the Second City");
String c2 = input.nextLine();
System.out.println("Enter the Third City");
String c3 = input.nextLine();
String temp;
// Example: c b a
// c > b
if (c1.compareTo(c2) > 0) {
temp = c1;
c1 = c2;
c2 = temp;
}
// b c a
// c > a
if (c2.compareTo(c3) > 0) {
temp = c2;
c2 = c3;
c3 = temp;
}
// b a c
// a > b
if (c1.compareTo(c2) > 0) {
temp = c1;
c1 = c2;
c2 = temp;
}
// a b c
System.out.printf("%s %s %s", c1, c2, c3);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With