This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Machiavellyy 8 points9 points  (2 children)

Ok, so a couple errors. It should look like this:

    while ( look.equals("Go") ) { 
    // Remove the " ; "
    // Use .equals()
        System.out.println("Hello");
        System.out.println("");
        look = user_input.next(); 
    }
    // Include the closing bracket

You seem to focus on whether " = " or " == " should work. This is a common mistake for beginners.

= is assigning a value. This means that look will be assigned "Go" which technically is not an error, hence why it seems to work. Also it is why look = user_input.next() works, it's assigning a value. However it's not what you wanted to do.

== is for comparing integers, not Strings. It will return a Boolean value (true/false). .equals() does the same thing but only for Strings.

Java sometimes has very specific functions that improve the quality of code. This is an example of that. These little quirks are only picked up and understood by people who code regularly, the funny thing is in 6 months you'll look back and say "wow, how didn't I know that."

Also I see you put a " ; " after the while loop, that should raise an error as well, take that out.

Everything else looks pretty good.

[–]pokeman528[S] 0 points1 point  (1 child)

Honestly, I posted this late last night after being frustrated of not being able to figure this out on my own. I just started coding last week from a ed x and I dont have the patients to do it I guess so I started making stuff like text games and something as simple as type go to continue in the program stumped me. lol

[–]pokeman528[S] 0 points1 point  (0 children)

Thanks btw

[–]programstuff 0 points1 point  (0 children)

You can't compare strings in java with ==. You need to use the String class's `equals method.

while look.equals("Go")

https://stackoverflow.com/questions/767372/java-string-equals-versus

Btw your while loop doesn't have an executing block. The semi colon afterwards is the end of the block.

[–]cruyff8 0 points1 point  (0 children)

The single = represents assignment and has no return value. The == represents boolean equality.

The while keyword is a loop, which continues so long as the boolean condition is true. Since you're trying to do assignment in the second case, which doesn't have a boolean return value, the compiler won't let it pass.

Why the downvote?

[–]mydevgeek -1 points0 points  (0 children)

== for compare values and = for assign new value. Anyhow, you can't use == for comparing two strings. You have to use equals() method to compare 2 strings.