-4

I want to know what is wrong with this :

if (q == "outside" && q1 == "not alive") {
System.out.println("bison");
}

This statement works perfectly:

if (q == "outside" && q1 == "alive") {
System.out.println("bison");
}

All of the if statements containing "alive" works perfectly, but all of the if statements containing "not alive" does not work.

This is an exercise which I have to do without using else if or else.

full code :-

import java.util.Scanner;

class age04 {
public static void main(String [] args) {

Scanner keyboard = new Scanner(System.in);

String q, q1,;

System.out.println("Two more questions, baby!");
System.out.println("Think of something and i'll try to guess it!");


System.out.println("Question 1) Does it stay inside or outside or both");
q = keyboard.next();
System.out.println("Question 1) Is it alive?");
q1 = keyboard.next();


if (q == "inside" && q1 == "alive") {
System.out.println("houseplant");
}

if (q == "inside" && q1 == "not alive") {
System.out.println("shower curtain");
}

if (q == "outside" && q1 == "alive") {
System.out.println("bison");
}
if (q == "outside" && q1 == "not alive") {
System.out.println("bison");
}

if (q == "both" && q1 == "alive") {
System.out.println("dog");
}

if (q == "both" && q1 == "not alive") {
System.out.println("cell phone");
}

}
}
JmJ
  • 1,989
  • 3
  • 29
  • 51
Ashish Gogna
  • 57
  • 2
  • 6
  • 4
    You are making a common Java beginner's mistake, that is comparing strings using `==`. You should use `.equals(...)` instead. – Jesper Mar 24 '14 at 13:17

4 Answers4

1

Use The equals() function to Compare a String in java the == function is not compare string :

          if (q.equals("outside") && q1.equals("not alive")) 
            {
             System.out.println("bison");
           }

Try This :

    import java.util.Scanner;

    class age04 {
    public static void main(String [] args) {

    Scanner keyboard = new Scanner(System.in);

   String q, q1;

   System.out.println("Two more questions, baby!");
   System.out.println("Think of something and i'll try to guess it!");


   System.out.println("Question 1) Does it stay inside or outside or both");
   q = keyboard.next();
   System.out.println("Question 1) Is it alive?");
  q1 = keyboard.next();


  if (q.equals("inside") && q1.equals("alive")) {
   System.out.println("houseplant");
   }

 if (q.equals("inside") && q1.equals("not-alive")) {
 System.out.println("shower curtain");
 }

 if (q.equals("outside") && q1.equals("alive")) {
 System.out.println("bison");
}
if (q.equals("outside") && q1.equals("not-alive")) {
System.out.println("bison");
}

if (q.equals("both") && q1.equals("alive")) {
System.out.println("dog");
 }

 if (q.equals("both") && q1.equals("not-alive")) {
 System.out.println("cell phone");
          }
     }
}
Benjamin
  • 2,257
  • 1
  • 15
  • 24
  • i used this for every if statement...... compiles perfectly.... but when it runs.... it only shows answer for if statments with "alive"...... this is seriously irritating.... please help..... – Ashish Gogna Mar 24 '14 at 14:16
  • thanks!!!! but is it possible to use spaces instead of dashes? – Ashish Gogna Mar 24 '14 at 15:59
0

Strings should compare like this,

q.equals("outside")
q1.equals("alive")
RKC
  • 1,834
  • 13
  • 13
  • I know it's overkill, but it'd be better if the answer would have been in the context of the question (put it in the `if..else`). – Hungry Blue Dev Mar 24 '14 at 13:19
0

== does not compare Strings, for that you'll want to use the equals() function instead.

equals() will simply return true or false depending on whether or not your string is the same as the input given to the function.

JmJ
  • 1,989
  • 3
  • 29
  • 51
0

Everyone else is correct but I want to clarify why you need to use equals in stead of == to compare strings.

In Java, unlike other languages, strings are immutable objects and not an array of char values. The == operator works correctly when comparing primitive values (e.g. int and char). However, when you use == in Java to compare objects, the operator works by comparing memory references, not the underlying value of the objects being compares. As a result, two string with the same value will not be equal according to the == operator unless the memory references associated with each object is the same. That's why == will work to compare char, since it is looking for equality in the primitive values, it will not work for a whole strings since there is no primitive value to compare.

The equals method is something that is common to all Object classes in Java and specifies how to check for equality between members of that specific class. The default implementation of equals can overidden so that different types of comparison can be used for different types of objects (e.g. compare employees by id number, compare products by sku, etc). I wouldn't go around overriding equals methods (it's a real pain to get right) but it helps to understand why using .equals will give a different answer from ==.

Rarw
  • 7,645
  • 3
  • 28
  • 46