5

Possible Duplicate:
How do I compare strings in Java?
Java string comparison?

import java.util.*;

public class whatever
{

    public static void main(String[] args)
    {

        Scanner test = new Scanner(System.in);
        System.out.println("Input: ");
        String name = test.nextLine();

        if (name == "Win")
        {
            System.out.println("Working!");
        }

        else
        {
            System.out.println("Something is wrong...");
        }

        System.out.println("Value is: " + name);

    }

}

The code above is pretty self-explanatory. I'm assuming == can only be used for numbers? I want "Working!" to be printed.

Community
  • 1
  • 1
user1706295
  • 107
  • 2
  • 3
  • 5

2 Answers2

9

== compares objects by reference.

To find out whether two different String instances have the same value, call .equals().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Just to mention, "Win".equals(name) will be a more secure solution in case if name is null. – Egor Sep 30 '12 at 15:08
5

You should use

if (name.equals("Win")){
    System.out.println("Working!");
}

Edit Suggested By RC in comments to avoid null problems:

if ("Win".equals(name)){
    System.out.println("Working!");
}
Mahmoud Aladdin
  • 536
  • 1
  • 3
  • 13