I'm trying to have the scanner scan for each city, analyze the first character and arrange it alphabetically. I couldn't figure out how to use a for statement for this task and I've googled a lot, but now I'm thinking that would've been the right option. Unless an If Else would work which even under correct conditions, it still runs else instead. If I could use a For() how would I use it, if not how else could I get this to work.
For the sample cities I used "Austin [enter] Chicago [Enter] Denver [Enter]".
edit: let me clarify I added the "Error" part of the else to test the If argument.
package homework2;
import java.util.Scanner;
public class OrderCities {
public static void main(String[] args) {
// Create a scanner
Scanner scanner = new Scanner(System.in);
// Prompt user to input cities
System.out.println("Please enter each city");
String c1 = scanner.nextLine();
String c2 = scanner.nextLine();
String c3 = scanner.nextLine();
int first = 0;
int i1 = c1.charAt(first);
int i2 = c2.charAt(first);
int i3 = c3.charAt(first);
if ((i1 > i2 && i1 > i3) && i2 > i3){
System.out.println(c1 + " " + c2 + " " + c3);
} else
System.out.println("Error");
}
}
So My Professor told me I had it correct and found that the signs [Greater than, Less than] should be swapped since the program reads them in Unicode. He added System.out.println(i1 + " " + i2 + " " + i3);
at
int i2 = c2.charAt(first);
int i3 = c3.charAt(first);
System.out.println(i1 + " " + i2 + " " + i3);
if (i1 < i2 && i1 < i3){
if (i2 < i3){
System.out.println(c1 + ", " + c2 + ", " + c3);`
to have it read out the Unicode. Thanks everyone for your help! Here's what my original code came out to (this is just for documentation incase any other beginner like me has similar issues):
` package homework2;
import java.util.Scanner;
public class OrderCities {
public static void main(String[] args) {
// Create a scanner
Scanner scanner = new Scanner(System.in);
// Prompt user to input cities
System.out.println("Please enter each city");
String c1 = scanner.nextLine();
String c2 = scanner.nextLine();
String c3 = scanner.nextLine();
int first = 0;
int i1 = c1.charAt(first);
int i2 = c2.charAt(first);
int i3 = c3.charAt(first);
System.out.println(i1 + " " + i2 + " " + i3);
if (i1 < i2 && i1 < i3){
if (i2 < i3){
System.out.println(c1 + ", " + c2 + ", " + c3);
}else
System.out.println(c1 + ", " + c3 + ", " + c2);
} else if (i2 < i1 && i2 < i3){
if (i1 < i3){
System.out.println(c2 + ", " + c1 + ", " + c3);
}else
System.out.println(c2 + ", " + c3 + ", " + c1);
} else if (i3 < i1 && i3 < i2){
if (i2 < i1){
System.out.println(c3 + ", " + c2 + ", " + c1);
}else
System.out.println(c3 + ", " + c1 + ", " + c2);
} else
System.out.println("ROFLOL, there seems to be an 3RR0R, which means you somehow found a way to break my code!! TR0LL0L0L0L0L");
}
}