-3

I am having a problem with parsings strings; I can't seem to find out why my String Array's values are not equal to what they should be! This may seem weird, so here is a simplified version of the code (The application is much larger, so I just made a small program to explain my point)

public class Test {

public static void main(String[] args) {

    String src = "beep zap derp flop";
    String delims = "[ ]+";
    String[] tokens = src.split(delims);

    if (tokens[0] == "beep") {
        System.out.println("you said beep!");
    } else {

               for (int i = 0; i < tokens.length; i++) {
                    System.err.println("'" + tokens[i] + "'");
               }
          }
     }
}

My output is

'beep'
'zap'
'derp'
'flop'

I know for a fact that tokens[0] is beep, but the Java does not seem to notice. Is there something I am doing wrong?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    http://stackoverflow.com/q/513832/758280 – Jeffrey Mar 21 '13 at 00:23
  • Tags are automatically added to the title as necessary, don’t add them manually. – Konrad Rudolph Mar 21 '13 at 00:27
  • 1
    @Matisse : since you got 3 answers and all are correct. Accept the one you want. You should accept answered questions. – Saher Ahwal Mar 21 '13 at 02:19
  • 1
    @Saher - 99% of the people who ask questions like this will never return to the site once they get their copy/paste answer. That's why these questions should be downvoted and/or closed... so they don't pollute the site with useless duplicates. – jahroy Mar 21 '13 at 02:21

4 Answers4

7

You should never use the == operator to compare the contents of Strings in Java. When used with Objects such as Strings, the == operator compares the references for equality. In other words, == will only return true for objects if both references refer to the same exact object. If you have two different String objects, then == will return false even if the contents are the same.

Use String#equals to compare the contents of Strings. This method will compare the contents of the String objects character-by-character to determine if the contents are identical.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Oh my gosh! Thank you so much, you have no idea how much this helped! Now I can finally attempt to make a horribly inefficient scripting language interpreter for my game engine! – Matisse Alexander Mar 21 '13 at 00:31
1

You should use .equals to compare Strings.

The equals method checks the actual contents of the strings, the == operator checks whether the two objects reference the same instance.

String.equals(String other)

See this: http://blogs.adobe.com/cantrell/archives/2003/04/how_equals_work.html

jahroy
  • 22,322
  • 9
  • 59
  • 108
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
1

In Java, you have to use equals method to compare two strings. For example, if you want to compare String a with String b, you can say if (a.equals(b)). The == operator checks if two strings are references to the same object.

tianz
  • 2,155
  • 2
  • 19
  • 28
0

public class Test {

public static void main(String[] args)    {
String src = "beep zap derp flop";
String delims = "[ ]+";
String[] tokens = src.split(delims);

if ("beep".equals(tokens[0]))
{
    System.out.println("you said beep!");
} 
else
{

           for (int i = 0; i < tokens.length; i++) {
                System.err.println("'" + tokens[i] + "'");
           }
      }

}

}

user1673787
  • 65
  • 1
  • 2
  • 8