1

I am developing a wicket client for my webservice.

On my first steps with wicket I noticed a strange behaviour. I have a form where I enter an username:

         <form wicket:id="registerForm">
            User Name <input type="text" wicket:id="userId"/><br/>
            <input type="submit" value="Register"/>
         </form>

My Submit for this looks like:

private TextField userIdField;
private Form form;


public Register() {

    userIdField = new TextField("userId", new Model(""));

    form = new RegisterForm("registerForm");
    form.add(userIdField);
    add(form);
  }


class RegisterForm extends Form {
  public RegisterForm(String id) {
    super(id);
  }
  @Override
  public void onSubmit() {

    String userId = Register.this.getUserId();


   if(userId == "test") {
       System.out.println("normal");
   }
   else {
       System.out.println("strange");
   }

  }
}



protected String getUserId() {
    return userIdField.getDefaultModelObjectAsString();

}

}

When I enter test in my form the console says "strange".

Whats wrong with it?

Xavi López
  • 27,550
  • 11
  • 97
  • 161
user1090145
  • 25
  • 1
  • 8
  • You have to check Strings with the `equals`-method if you want to check the equality of the content. See also [here](http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html) – rotsch Jan 09 '12 at 22:53

1 Answers1

3

Remember that checks for equality in Java regarding Strings should use the String.equals() method. Using == will only return true when both operands are the same instance.

if (userId.equals("test")) {
    System.out.println("normal");
}

This question might be useful to you: How do I compare strings in Java?

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161