0

I'm importing a string from a file and the string is "Computer_Made". If I execute this code, though, it does not print "The computer is already made!" Any ideas?

if (data=="Computer_Made")
    {
    computer=true;
    System.out.println("The computer is already made!");
    }
APCoding
  • 303
  • 2
  • 19
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Patrick Roberts May 15 '15 at 00:07

2 Answers2

2

You should use .equals for string comparison!!

if (data.equals("Computer_Made"))
{
computer=true;
System.out.println("The computer is already made!");
}

Refer here for more info

Community
  • 1
  • 1
user4655509
  • 81
  • 1
  • 8
1

In Java, String are compared using equals or equalsIgnoreCase method.

Using == is reference equality and will rarely be the same.

Try instead:

if (data.equals("Computer_Made"))

== will only work in example like this:

String a = "Ha";
String b = a;
System.out.println("a==b :" + a==b); //prints a==b : true
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82