-1

I'm trying to make a basic "20 questions" type of thing to learn how to use if statements with boolean comparators like && in them. However, my "if" statements are not, uh, "doing" (sorry) even though their criteria are being satisfied (as far as I can tell).

When I compile, no matter what "answers" I input, the only output I get is "I'd ask you if I'm right..." ALA:

Think of an object, and I'll try to  guess it!
1. Is it an animal, vegetable, or mineral?vegetable
Is it bigger than a breadbox?yes
I'd ask you if I'm right, but I don't really care

I tried googling and searching but I feel like I'm missing out on something so basic that I'm just not seeing it. Here's the code:

Scanner keyboard = new Scanner(System.in);
    String response1, response2;

    System.out.println("Think of an object, and I'll try to "
            + " guess it!");
    System.out.print("1. Is it an animal, vegetable, or mineral?");
    response1 = keyboard.next();

    System.out.print("Is it bigger than a breadbox?");
    response2 = keyboard.next();

    if(response1 == "animal" && response2 == "yes")
    {
        System.out.println("You're thinking of a moose");
    }
    if(response1 == "animal" && response2 == "no")
    {
        System.out.println("You're thinking of a squirrel");
    }
    if(response1 == "vegetable" && response2 == "yes")
    {
        System.out.println("You're thinking of a watermelon");
    }
    if(response1 == "vegetable" && response2 == "no")
    {
        System.out.println("You're thinking of a carrot");
    }
    if(response1 == "mineral" && response2 == "yes")
    {
        System.out.println("You're thinking of a Camaro");
    }
    if(response1 == "mineral" && response2 == "no")
    {
        System.out.println("You're thinking of a paper clip");
    }

    System.out.println("I'd ask you if I'm right, but I don't really care");

Thanks in advance to any respondents!

Caleb Jay
  • 2,159
  • 3
  • 32
  • 66
  • 1
    Cause of all string comarison problems.. [How do i compare the Strings???](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sanbhat Jul 22 '13 at 09:11

4 Answers4

1

You have to compare strings like

if(response1.equals("animal")){

// do something 
}

== compares the exact values. So it compares if the primitive values are the same,

String#.equals() calls the comparison method of objects, which will compare the actual objects pointed by the references. In the case of Strings, it compares each character to see if they're equal.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You should use equals() to compare Strings not ==.

Using your example:

if(response1.equals("animal") && response2.equals("yes"))
{
    System.out.println("You're thinking of a moose");
}...
pushy
  • 9,535
  • 5
  • 26
  • 45
  • Well, that did fix it, but why can't we use == for strings? EDIT: Ah, I found the big answer thread answering this. Thanks! – Caleb Jay Jul 22 '13 at 09:16
0

The problem with your code is related to string comparisons and not with '&&' operator. Use equals method for string comparison. '==' checks whether two references are pointing to the same memory object.

Replace your if check(s) for string comparison

from

if(response1 == "animal" && response2 == "yes")

to

if("animal".equals(response1) && "yes".equals(response2))

Here is a related post to understand more about string comparison in java

Java String.equals versus ==

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Compare String values use equalsIgnoreCase

if(response1.equalsIgnoreCase("animal")){

 //process
}

conditional check for boolean values here

newuser
  • 8,338
  • 2
  • 25
  • 33