Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

Hi, Java question. Been 2 years since I was here, I forget a lot... if statements. A simple code, it isn't entering into my if statement, not sure why or maybe it has to do with changes since last time I was using java 6... will post code in comments.

OpenStudy (anonymous):

if ( (type == "animal") && (size == "y")){ guess = "moose"; }

OpenStudy (anonymous):

Even just this isn't going into the statement...: if (type == "animal" ){ guess = "moose"; }

OpenStudy (anonymous):

However the code works if I just type if(true){ guess = "moose"; } So can't I use a boolean and a string?

OpenStudy (anonymous):

OK I found the answer, I need to use: int animal = type.compareTo("animal"); if(animal == 0){ guess = "moose"; }

OpenStudy (woodrow73):

When testing for String equality, it is paramount to use str1.equals(str2), instead of str1 == str2

OpenStudy (woodrow73):

Furthermore, if you wish to ignore caps, str1.equalsIgnoreCase(str2) will handle that for you.

OpenStudy (woodrow73):

And welcome back to java lol.

OpenStudy (anonymous):

Thankyou :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!