Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is equal to a string [duplicate]

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.

like image 242
user1706295 Avatar asked Dec 09 '22 20:12

user1706295


1 Answers

== compares objects by reference.

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

like image 188
SLaks Avatar answered Dec 27 '22 05:12

SLaks