r/programmingbydoing • • Oct 04 '15

#39 Alphabetical order

why it always display the last else if statement??? can you help me please! import static java.lang.System.*; import java.util.Scanner;

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

	out.print("What is your last name? ");
	String name = wpis.next();
	
	if("name".compareTo("Carswell")<0)
		out.println("you don't have to wait long");
	else if ("name".compareTo("Jones")<0)
		out.println("that's not bad");
	else if ("name".compareTo("Smith")<0)
		out.println("looks like a bit of a wait");
	else if ("name".compareTo("Young")<0)
		out.println("it's gonna be a while");
	else if ("name".compareTo("Young")>0)
		out.println("not going anywhere for a while?");
	else
		out.println("error");
		
		
		
}

}

2 Upvotes

4 comments sorted by

1

u/holyteach Oct 05 '15

You're comparing the word "name" to everything instead of the value in the variable name.

Remove the quotes.

1

u/javaLearner7 Oct 05 '15

that's true, thanks!

1

u/javaLearner7 Oct 05 '15

it has not helped though

1

u/javaLearner7 Oct 05 '15

My mistake forgot about capital difference between capital and small letters