tags:

views:

546

answers:

8

Ok, this is stupid, but wtf is going on?

I have a String variable in a servlet, which takes the value of a parameter and based on that value I make a test to do something, but the if is not working. What is the problem?

 String action = request.getParameter("action");
    System.out.println("Action: " + action);
// I put 2 ifs to be sure, but not even one is working
    if(action.equals("something"))
            {
                System.out.println("hey");            
            }
    if(action.trim() == "something")
            {
                System.out.println("hey");
            }

On the console, the System.out.println shows me that the value of action is "something"

Action: something
+18  A: 

Your second comparison is wrong. You should also use equals instead of ==, like this:

if (action.trim().equals("something"))

The == operator compares references of (String) objects and under normal circumstances equal strings don't automatically have the same reference, i.e. they are different objects. (Unless both are internalized, but normally you shouldn't consider it)

Other than that your example works fine and the first comparison is valid. Try fixing the second comparison. If it works, you found your problem. If not, try using a debugger and double-check everything.

PS: When comparing literal strings with dynamic string objects, it's good practice to call the equals method on the literal string:

"something".equals(action)

That way you can avoid NullPointerExceptions when the string object is null.

DR
+1 for calling equals on string literal to avoid NullPointerException.
Steve Claridge
A: 

The equals method compares the strings for object identity and compares not the content. To compare the content of two strings, use the compareTo method.

Frank
This is plain wrong. `String.equals()` compares the strings and returns `true` if they are equal—just as one would expect.
Bombe
A: 

Just a wild guess: could be that one of these "somethings" contains e.g. a Cyrillic character that looks identical to its Latin counterpart. In this case, it could be the "o".

David Parunakian
+5  A: 

Your second condition is unlikely ever to be true - you're testing whether the string object created by trimming action is the same object as the string literal "something". This will only be true if action is set to that same literal value elsewhere. Use "something".equals( action.trim() ) instead.

Your first condition would be true the characters in the action string are the characters "something". If it's not true, then it doesn't. Assert it in a test, log it, print it or look at it in a debugger.

If you print a string for debugging, use something like System.out.println ( "String = >" + string + "<" ); so that it's obvious if there are any trailing spaces.

Pete Kirkham
Your answer should be preferred, it's more correct and even avoids NPE:s (in theory, not in this exact case tho'). Oh well, here's to hoping loads of other peoples notice that too.
Esko
+3  A: 

String comparison in Java cannot be done by ==

You have to use String.equals() or String.compareTo()

By the way compareTo returns 0 where String.equals() return true if two strings are equal.

See: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals%28java.lang.Object)

JCasso
+1  A: 

My guess is that you've got trailing or leading spaces which don't show in the println.

Combine this with the fact that you're using action.trim() == 'something' means this test won't work either.

Switch this to .equals("something") as suggested by others and it might work.

Chris R
I often do `System.out.println("Action: [" + action + ']');` for debugs as then you can see the trailing spaces.
Chris R
Sorry that should be `System.out.println("Action: [" + action + "]");`. I've been writing JavaScript this morning :)
Chris R
+1  A: 

You can better protect against nulls by switching the if clauses around to have the string literal first.

But since you also seem to want to protect against whitespace on the parameter value, you could also do a null safe trim of the parameter value using StringUtils.trimToEmpty from Apache Commons Lang:

String action = StringUtils.trimToEmpty(request.getParameter("action"));

System.out.println("Action: " + action);

if("something".equals(action)) {
   System.out.println("hey");            
}
A_M
A: 

try this :

String action = request.getParameter("action"); System.out.println("Action: " + action);

if(action.trim().equals("something"))
        {
            System.out.println("hey");            
        }