Actually the test that you want is:

        if (user.compareTo("admin") == 0) {
                ...
        }


-----Original Message-----
From: Cliff Rowley [mailto:[EMAIL PROTECTED]]
Sent: Saturday, November 18, 2000 7:35 AM
To: [EMAIL PROTECTED]
Subject: RE: getParameter is NOT a string?


Because object variables in Java are references, when you use == you are
comparing those references, not the objects or values themselves.  For
example, in your code:

  if ( user == "admin" )

Here you are comparing the reference to the user object with the reference
to the string "admin".  It is very unlikely that the references are
identical, so the test fails.

Instead, use the equals() method to determine equality, so your code
becomes:

  if ( user.equals( "admin" ) )

That should work.  Note that this applies only to object types, not
primitives.

HTH

Cliff Rowley

The reader this message encounters not failing to understand is cursed.

- while ( !asleep ) { code(); }

-----Original Message-----
From: Winnie Cheung [mailto:[EMAIL PROTECTED]]
Sent: 18 November 2000 05:55
To: [EMAIL PROTECTED]
Subject: getParameter is NOT a string?


Hi I am getting a form field (POSTed form) as

    String user = (String) request.getParameter("user");

When I out.print this, it writes the correct value, but when i COMPARE this
in an IF condition, it doesnt work. Any reason why? For example, lets say
the "user" textfield in my form is "admin".

Now when I grab it using getParameter, the value printed out is admin!! But
when I do this condition:

    if (user == "admin")
    {
    }

then it doesnt go into this condition, but goes into the ELSE instead!!!

Why is this?

Would appreciate any help..thanks!!



Reply via email to