#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    2
    Rep Power
    0

    Problem with type?


    I've new to jsp but have been forced to alter some pages.

    I have a variable called dsu, I know the value of dsu is P25 but can't get this to work -

    String dsu = item.getDefaultSalesUnitCode();
    if(dsu == "P25")
    {
    out.println("TEST");
    }

    I take it that dsu is a string? I've tried explicitly converting to a string but it makes no difference. Am I doing something wrong?

    I'm new to this so hopefully it's something simple, any ideas?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    13
    You have fallen into the classic beginner Java trap, dont feel bad it happens to everyone at the beginning. The == comparison operator should only be applied to numberic values or to test if two objects are the same, meaning they are references to the same object in memory. To compare Strings ALWAYS use the .equals() method so in this case the code should be
    if (dsu.equals("P25")).
    DC Dalton
    DCD Designs
    SCJP
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    2
    Rep Power
    0
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo