I'd do something similar to Tim's answer. But to do things the way you were trying to do them, you have a lot of problems that need to be fixed:
(1) String literals in Java are surrounded by double quote marks, not single quote marks.
(2) user_input
is a Scanner
. You can't compare a scanner to a string. You can only compare a String
to another String
. So you should be using answerString
in your comparison, not user_input
.
(3) Never use ==
to compare strings. StackOverflow has 953,235 Java questions, and approximately 826,102 of those involve someone trying to use ==
to compare strings. (OK, that's a slight exaggeration.) Use the equals
method: string1.equals(string2)
.
(4) When you write a do-while
loop, the syntax is do
, followed by {
, followed by the code in the loop, followed by }
, followed by while(condition);
. It looks like you put the last }
in the wrong place. The }
just before the while
belongs to the else
, so that doesn't count; you need another }
before while
, not after it.
(5) I think you were trying to write a loop that keeps going if the input isn't yes
or no
. Instead, you did the opposite: you wrote a loop that keeps going as long as the input is yes
or no
. Your while
condition should look something like
while (!(answerString.equals("yes") || answerString.equals("no")));
[Actually, it should be equalsIgnoreCase
to be consistent with the rest of the code.] !
means "not" here, and note that I had to put the whole expression in parentheses after the !
, otherwise the !
would have applied only to the first part of the expression. If you're trying to write a loop that does "Loop until blah-blah-blah", you have to write it as "Loop while
!
(blah-blah-blah)".