2

in this homework i have to do a predicate method that prints a question and then waits for a question. if the user enters no, the method should return false, if the user enters yes the method should return true. I have done that ! but in this part i have problems: if the user enters another thing the program must say something like "wrong answer" and repeat the question. I can't return a string because is a boolean method and i dont know how to resolve this. Thank you!!

import acm.program.ConsoleProgram;
public class YesNo extends ConsoleProgram{
    public void run () {
   String answer = readLine ("would you like instructions? ");
   println (StrBoo (answer));
}


private boolean StrBoo(String answer){
    if (answer.equals("yes")) {
        return true;
    } else if (answer.equals("no")) {
        return false;
    } else { 
    return false;
    }   

}
}
3
  • Use a do-while loop... Commented Aug 18, 2014 at 2:02
  • Take in input, check if it is either yes or no, and if it is call your method, if not tell user they entered wrong and prompt them again. Like Luiggi says, you can use a do while for this. Commented Aug 18, 2014 at 2:03
  • Boolean method name should start with "is" , "has", "can", "should".Refer [wiki.eclipse.org/Recommenders/CodingConventions] Commented Dec 7, 2017 at 6:45

2 Answers 2

2

First StrBoo is a poor method name. I would call it getAnswer(), and use something like,

private static boolean getAnswer() {
  while (true) {
    String answerStr = readLine ("would you like instructions? ");
    answerStr = (answerStr != null) ? answerStr.trim() : "";
    if ("yes".equalsIgnoreCase(answerStr)) {
      return true;
    } else if ("no".equalsIgnorecase(answerStr)) {
      return false;
    } else {
      System.out.println("Wrong answer");
    }
  }
  return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I guess that it's a poor name but now you're breaking encapsulation. Seems like this method should only receive the String as parameter and validate if the string is "yes" or "no" (as shown from the beginning).
@LuiggiMendoza I disagree. The problem as originally phrased also breaks encapsulation, because now the input validation is happening away from the input. I suppose getAnswer() could return null when the input is invalid. As is you'd need 3 values. true, false and not a value. Or you could throw an Exception I guess.
Ok I re read it and this seems to be a right implementation. And your statement about returning three values can be met by using Boolean :P (troll part of the comment).
0

The correct design for such kind of program is to throw an exception. Here is an example:

import acm.program.ConsoleProgram;

public class YesNo extends ConsoleProgram 
{
    class WrongAnswerException extends Exception
    {
    }

    public void run ()
    {
        try
        {
            String answer = readLine("would you like instructions? ");
            println(StrBoo(answer));
        }
        catch(WrongAnswerException e)
        {
            println("You have to write yes or no!")
        }
    }

    private boolean StrBoo(String answer) throws WrongAnswerException
    {
        if ("yes".equals(answer))
        {
            return true;
        }
        else if ("no".equals(answer))
        {
            return false;
        }
        else
        {
            throw new WrongAnswerException()
        }
    }
}

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.