4

I'm trying to learn java but I'm stuck trying to do a single program which concerns Do While Statement with two conditions. Specifically, I want a method to run until the user write "yes" or "no". Well, down there is my thing, what is wrong with it?

    String answerString;
    Scanner user_input = new Scanner(System.in);
    System.out.println("Do you want a cookie? ");

    do{
    answerString = user_input.next();
    if(answerString.equalsIgnoreCase("yes")){
        System.out.println("You want a cookie.");
    }else if(answerString.equalsIgnoreCase("no")){
        System.out.println("You don't want a cookie.");
    }else{
        System.out.println("Answer by saying 'yes' or 'no'");
    }while(user_input == 'yes' || user_input == 'no');
    }
}}

2 Answers2

4

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)".

ajb
  • 31,309
  • 3
  • 58
  • 84
  • 2
    I always appreciate an educative answer. :) One missing best practice: when comparing a literal and a variable, put the literal first (`"yes".equalsIgnoreCase(answer)`) as this avoids exceptions in case the variable is null. Doing it even when you know it's not is good because: a - it reinforces the habit for when you don't know, so you don't spend useless thinking on details; b - you never know how the code may evolve. – Chop Nov 04 '15 at 08:59
  • I usually do put the literal first, but five lessons in one post is enough for someone who's just learning Java. Actually, I question the part about "useless thinking". You should know whether your variables can possibly be null or not. And if it's a method parameter, whether a parameter is allowed to be `null` should be part of the contract between the method and its caller. Some languages like Swift force you to think about whether a value can be null (undefined) by using a different syntax, and I think that's a good thing. – ajb Nov 05 '15 at 05:44
  • Useless thinking: yes, I agree. But not all developers _do_ know, think about it or even care (I'm growing tired of working with developers who copy-paste without thinking about consequences in their own context or useless duplications when they copy code from elsewhere in the application). The not-thinking-about-it habit might have problematic consequences, especially when providing an API to clients. That's why I've taken the habit of advising doing it systematically, to make it a reflex that doesn't require any thought. – Chop Nov 05 '15 at 06:58
2

I might opt for a do loop which will continue to take in command line user input until he enters a "yes" or "no" answer, at which point the loop breaks.

do {
    answerString = user_input.next();

    if ("yes".equalsIgnoreCase(answerString)) {
        System.out.println("You want a cookie.");
        break;
    } else if ("no".equalsIgnoreCase(answerString)) {
        System.out.println("You don't want a cookie.");
        break;
    } else {
        System.out.println("Answer by saying 'yes' or 'no'");
    }
} while(true);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • If using `while(true)`, we may be tempted to change the do..while to a traditional while, as the first is often described as 'brain overload'. – Chop Nov 05 '15 at 06:55