Ask coding questions

← Back to all posts
I have no idea what i did wrong
xMisbahx (14)

As you can probably see, I am very bad at coding (I'm still learning) and if..else and booleans are not my strong suit. If you could please point out my error/the answer to my error, please tell me!
Also if you tell me any others that you find. Thanks!

Answered by SixBeeps (5332) [earned 5 cycles]
View Answer
SixBeeps (5332)

if-statements evaluate whatever is in the parentheses and executes its code if the value happens to be true. Think of it as a math expression, like 7+4*8, except instead of simplifying to a number, boolean expressions simplify to either true or false. Simple enough, as you're probably familiar with this already.

In your if-statement, you're checking to see if true is either true or false. Since there is never a case where true directly evaluates to false (When would 7 ever equal 2, following the math analogy?) that code will always run no matter what, and the else gets completely ignored.

It looks like you're checking to see if String yesno is either the string "yes" or the string "no" and working with that. To check if one string is the same as another string in Java, you use equals() on the String:

if (myString.equals("hey")) {
  System.out.println("oh hi");
else {
  System.out.println("im just a computer who cant understand things");

Furthermore, you don't need the { } around the stuff after String yesno = scan.nextLine();. That doesn't do what I think you think it does.

Hope this helps!

xMisbahx (14)

@SixBeeps thank you thank you thank you!!! This helped so much. you deserve some stars *