1

Trying to write a program that asks the user if they want instructions. If the user enters maybe the console responds with "please enter a yes or no answer" and repeats the question.

I'm having difficulty listing the method parameters for the method askYesNoQuestion. Are the parameters simply String yes, String no? I am also stuck on how do I make the program repeat the question after someone enters maybe? Does my code look generally correct for what I'm trying to do?

import acm.program.*;



public class Instructions extends ConsoleProgram{

    public void run(){
        String answer  = readLine("Would you like instructions? : ")
        {
            if (answer.equals("maybe")) {
                println ("Please enter a yes or no answer.");  
            }
        }
    }   

    private boolean askYesNoQuestion(String yes, String no ){

        if (askYesNoQuestoin ("would you like instructions? "))
            if (answer.equals("yes")) {
                return yes;
            } else {
                if (answer.equals("no"))
                    return no;
            }
    }
1
  • Note: "yes" and "no" are not booleans. You're gonna have a heck of a time trying to return them. Commented Nov 1, 2012 at 3:14

5 Answers 5

1

Up to you how you do it, but really you are trying to convert a user's string input to something a bit easier for Java to work with.

I'd suggest askYesNoQuestion() would take the question to ask and then return true for yes and false for no. If you really want to handle "maybe" then use and int (or better yet an enum) to handle the response.

boolean askYesNoQuestion(String question)
{
     while(true) {
     // print the question
     // get the answer
     // if answer.equals("yes") return true 
     // if answer.equals("no") return false
     // any other answer asks the question again
     }
     return false;
}

// call looks like
if (askYesNoQuestion("Do you want instructions?")) {
  // do instructions
}
// Do the rest of the app.
// 
Sign up to request clarification or add additional context in comments.

2 Comments

Better than an int return, i think, would be for the function to only accept "yes" or "no" answers; ambiguous responses could trigger the function itself to print a message and try again. Considering the function's called askYesNoQuestion, i think it makes more sense that you only get a yes/no answer.
OP was talking yes/no/maybe. If you want the method to give you "yes/no/maybe" answers, then a boolean won't do it. I've tweaked the pseudo code to be a better match to the original question.
1

Firstly, pass the question, not the answer (you don't know the answer yet; you only know the question), so your method should look like:

private boolean askYesNoQuestion(String question)

next, loop until you get a yes or a no response:

private boolean askYesNoQuestion(String question) {
    println (question);
    while (true) {
         String answer = // read answer
         if (!answer.equalsIgnoreCase("yes") && !answer.equalsIgnoreCase("no")) {
             println ("Please enter a yes or no answer.");
         } else {
             return answer.equalsIgnoreCase("yes");
         }
    } 
}

Comments

0

If you are using Java 7, the Switch statement now supports Strings. I mean:

switch(inputString){
case "yes": /*do something */ break;
case "no": /*do something */ break;
case "maybe": /*do something */ break;
default: /*do something */ break;
}

So based on your requirement, you can write corresponding cases. For instance, you can display the message "enter yes or no" if user inputs "maybe" or something other than "yes" or "no" (using case "maybe" and default). Ideally, "maybe" is not required. the default case is used to handle this.

You can enclose this in a do-while loop and based on your condition, choose to continue or break

Comments

0

I don't know about your readline method, but I assume it's valid. So your run method should be :

public String run(){
   while(true) {
      String answer = readLine("Would you like instruction?: ");
      if (answer.equals("maybe") {
          System.out.println("Please enter a yes or no answer");
      }
      else {
          return answer;
      }
   }
}

So, when this method is run, it will loop until the user doesn't type "maybe" (and hope they will type yes or no).

3 Comments

If ConsoleProgram inherits its run method from Runnable, changing the return type isn't really an option.
if that true, so the original method (void run) is useless :)
Yeah, this whole class is a bit weird, though. Really, i'd think it ought to output the instructions if the user wants them. If that's not its job, then the design seems pretty confused. A whole class just to print a specific question and get a yes/no answer? That might be "maybe"? erm...
0

Well...there's a lot going on here. Don't feel bad about this, but it wouldn't work in this state - the compiler would throw a lot of stuff at you. Let's look at it step by step.

Before we look at syntax, let's look at program flow. What do we want to do? From your description you want to accomplish the following:

  • Have a user respond to a question for instructions.
  • If they reply with yes, then give them the instructions and exit.
  • If they reply with no, then exit.
  • If they reply with maybe, clarify that they should only have a yes or no answer, then ask your question again.

Now, we can look at syntax to accomplish this. In general, we will need a way to capture the user's response, and use that to decide what to do next. readLine() isn't what you're going to use (since that method doesn't exist); you'll want to use Scanner. The documentation in the API should be enough to get you started.

Next, now that we have the input, we need a way to repeat if the user doesn't answer yes or no. You have a choice - either a while loop or a do...while loop will work. We would have to check whether or not our condition is met, and if it isn't, we keep going.

Third, the string "yes" is different from "Yes" and "YeS" and "YEs". Case matters with strings, so the idea to compare them would be to equalsIgnoreCase().

Fourth, the method being created should only take in a single parameter - what the response is. You don't know what that will be until you actually do work on it - which is an if else-if else-if else statement. I see no reason for that method to call itself.

Finally, you don't call the method anywhere in run! You have to do that or it won't really matter what that method does; as long as it's syntactically correct, Java will happily ignore it if it's never called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.