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.
if ( (type == "animal") && (size == "y")){ guess = "moose"; }
Even just this isn't going into the statement...: if (type == "animal" ){ guess = "moose"; }
However the code works if I just type if(true){ guess = "moose"; } So can't I use a boolean and a string?
OK I found the answer, I need to use: int animal = type.compareTo("animal"); if(animal == 0){ guess = "moose"; }
When testing for String equality, it is paramount to use str1.equals(str2), instead of str1 == str2
Furthermore, if you wish to ignore caps, str1.equalsIgnoreCase(str2) will handle that for you.
And welcome back to java lol.
Thankyou :)
Join our real-time social learning platform and learn together with your friends!