简体   繁体   中英

Why doesn't my string comparison work?

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

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, ie 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.

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.

String comparison in Java cannot be done by == .

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

By the way, String.compareTo() returns 0 whereas String.equals() returns true when two strings are equal.

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

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.

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");            
}

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

try this :

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM