Hey guys, yes the title sounds funny, I am working on this project for a special someone, and I want to ask her out by writing her a simple program. I am having some trouble though with the execution of this idea. I have a pseudo code ish plan in my mind but need help executing it. I was planning on having her input yes or no answers to questions in the console and have the program respond with answers based on her inputs. my Idea was her input yes or no answers to questions, and then say something like If input is "yes" then print this statement, else if "no" print this statement. I am currently having problems getting the if else part to work. I cant have strings act like booleans in an if statment, so I need some help with that. code I started with:

import java.io.*;
public class ask {
    public static void main(String [] args) throws IOException {
        BufferedReader in = new BufferedReader
                (new InputStreamReader(System.in));
        String answer;
        System.out.print("Would you like to go out with me? ");
        answer = in.readLine();
        Boolean hope = Boolean.valueOf("yes");
        if (answer == hope) {

            System.out.print("Not awesome!");


obviously Im not actually using that question those responses, im just putting it as in example just to get the program itself to work. I cannot compare answer to hope because one is boolean and the other is a string, I also cannot do string with string, so how do I get this to work. Any ideas?

4 Years
Discussion Span
Last Post by bguild

A boolean is true or false, not text, so there's really no hope for doing a comparison like answer == true or answer == false; since answer is a bit of text it will never be true or false. What you want is more like answer == "yes" because both answer and "yes" are text, except that in Java that won't work either because == is only true if both sides are the same object, and the same text can be represented by two different String objects.

To test if two String objects contain the same text, you should use the equals method, as in answer.equals("yes") or "yes".equals(answer). But you want to be a bit more aggressive than that. You wouldn't want to take "Yes" as a no, but "yes".equals("Yes") would fail. Instead, you should use "yes".equalsIgnoreCase(answer).

Even that isn't really good enough, because "yes".equalsIgnoreCase("yes ") would fail because there is an extra space at the end of the second yes. Fortunately, the trim method creates a new String with spaces removed from the beginning and end, so you can do "yes".equalsIgnoreCase(answer.trim()) and that should cover any extra spaces that might get in there.

You probably want to also test for "y", "ya", "yeah", "yep", "okay", "sure", "fine", "true" and other similar things, too. And if the answer is none of those things, then you probably want to test that the answer is "n", "no", "nope", "never", "no way", "negative", "false" and other similar things, just to be sure. If the answer is neither positive or negative, then you should have it say that it doesn't recognize the answer, or ask, "Is that a yes or a no?" and use a loop to bring the program back around to accepting input again. That way you are protected from mistaking the meaning of whatever is typed.

You can also use a loop to help you check for each of the many forms of "yes" and "no" if you put the forms into a yes-array and a no-array.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.