2

Possible Duplicate:
Java String.equals versus ==

I have been having difficulties using nextLine() to get a string, and then use it as a test condition (either in an if statement or a while loop). Looking at the println(), it seems as if the String is correctly assigned to the variable 'repeat' but then the test condition fails for some reason. Banging my head on the wall, bleeding from my forehead. Please help.

import java.util.Scanner;

public class potpie {

    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        String repeat = "yes";


        System.out.println("Type in yes");
        repeat = input.nextLine();
        System.out.println("If repeat is now yes, print yes:  " +repeat);

        if(repeat == "yes"){
            System.out.println("It worked");
        } else
            System.out.println("it failed");
    }
}
Community
  • 1
  • 1

3 Answers3

2

You should use equals. == provide you reference equality and equals provide you value equality.

if("yes".equals(repeat)){ 

instead of

if(repeat == "yes"){

I would advice you to get eclipse/net beans and start debugging or a simple search would have resulted in the answer

Java Debugging with Eclipse - Tutorial

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
1
if(repeat == "yes"){

should be

if(repeat.equals("yes"){

(or)

 if("yes".equals(repeat){

Every day we see this question lot of times, simple search could have provided you sufficient information.

== equals for primitive comparison (reference equality). equals() is for String (or) Object comparison (object content equality).

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 2
    Yes, but it is sometimes hard to know what to search for. – JohnB Oct 31 '12 at 20:41
  • I sort of disagree. What exactly one would google for in this case? I would actually argue that Java should fix this bug. Yes, it will break some poorly written software. – MK. Oct 31 '12 at 20:41
  • 1
    May be String equality? OP knows test condition is failing. – kosa Oct 31 '12 at 20:42
  • @JohnB I agree with the sentiment. On the other hand, learning to use a debugger is an essential skill. IMO, advising someone in debugging skills is better than telling them to do a google search (at least in this particular instance). – Code-Apprentice Oct 31 '12 at 20:46
  • Of course. Nevertheless, if you are new to the language, you might not understand why `==` fails and might not think about reference equality, depending on where you are coming from. The behaviour of the `==` operator in Java when applied to strings is logically perfectly consistent, but, honestly, not very useful. – JohnB Oct 31 '12 at 20:48
  • it is not a bug. While other languages might use a `Equals` and `ReferenceEquals` method, Java very eloquently allows the equality operator (`==`) to always have the same meaning. It would be more confusing to hack in a special meaning for `==` with `String`. Also, we happen to just see this question a lot because `String` just happens to be the object intro programmers are using for their first object equality comparison. Actually, it is quite nice to provide this avenue to learn early the difference between `==` and `Object.equals`. – Tim Bender Oct 31 '12 at 20:50
  • @all, I am not telling it is wrong to ask (I didn't close or -ve vote), All I am telling is simple search might give some clues, lot of questions with almost exact syntax. – kosa Oct 31 '12 at 20:50
  • Thank you everyone for your quick and helpful responses. I spent some time looking at StackOverFlow posts to try and solve the issue myself before posting. That said, knowing what exactly to search for in this case proved to be difficult. Someone suggested I could have used the debugger to solve this issue. Would I have been able to solve this using solely the debugger? – Java Jabroni Oct 31 '12 at 21:16
  • @user1789696: I think using debugger you could have atleast narrowed down the issue to specific line (even though it don't tell you exact issue). Then search on SO like == condition failing etc., might have definitely helped you. Well don't worry you will learn all these with experience. – kosa Oct 31 '12 at 21:19
1

Sometimes == should be used for objects, but what it is actually comparing is whether a and b are literally the same object (have the same address in memory). As the others have said, you are comparing content in this situation, so you use .equals()

TLive
  • 53
  • 1
  • 5