-2

Possible Duplicate:
How do I compare strings in Java?

User sends command "/test " to get 2 outputs: - if password was wrong - if password was correct

The correct password is "secret"

Why when user do "/test secret" it still returns that its wrong password?

@Override
    public void processCommand(ICommandSender sender, String[] args)
    {  
        if ( args.length == 1 )
        {      
            if ( args[0] != "secret" ) {
                sender.sendChatToPlayer("Wrong password " + sender.getCommandSenderName() );
            } else {
                sender.sendChatToPlayer("Welcome " + sender.getCommandSenderName() );
            }
        } else if ( args.length == 0 ) {
                sender.sendChatToPlayer("No parameters send");
        } else {
                sender.sendChatToPlayer("You have to give me just 1 argument");
        }
    }
Community
  • 1
  • 1
John Smith
  • 321
  • 1
  • 3
  • 10

2 Answers2

4

Because they are different instances. Use equals() to compare strings.

Val
  • 1
  • 8
  • 40
  • 64
0

You should use string.equals(other_string) instead of ==, which only checks they are the same object. They may not be the same object, but may still be equal. String interning may happen in Java where the two objects are the same, but this is not guaranteed.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84