Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: .equals does not work

  1. #1
    mpiaser is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default .equals does not work

    I am struggling with something that should be simple and I hope somebody here can show me the error of my ways. All I am trying to do is to make a section of code execute if two strings are equal. The two strings are userId and "A001062". When I use the debugger in Eclipse, I can see the value of userId as "A001062" but whatever string comparison I try never evaluates to true. I have tried
    userId=="A001602"
    userId.equals("A001602")
    "A001602.equals(userId)
    Assigning A001062 to a string called AAA and comparing userId to AAA

    My code is as follows. I have also attached a screen shot from the Eclipse Debugger which makes me think the string comparison should succeed. I never see the debugger execute the print line nor do I see the print line on the JBOSS console.

    String userId = StringUtils.trim(nextLine[HR_USER_ID]);
    String AAA="A001062";


    if (userId.intern().equals(AAA.intern())) {System.out.print("MKP1: " + userId+"-"+managerId);}
    if (userId.compareTo("DTS0428")==0) {System.out.print("MKP2: " + userId+"-"+managerId);}
    Attached Thumbnails Attached Thumbnails .equals does not work-capture.jpg  

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: .equals does not work

    Also at www.javaprogrammingforums.com

    If a discussion is initiated at multiple places you ought to post links at each to the others so that anybody that takes part knows what else is being said. Be aware that some people won't bother thinking about a question that might have been answered elsewhere.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: .equals does not work

    This is simple enough that you can post some code to demonstrate that it does not return true after you have initialized all values. Please make certain the code is completely compilable without errors.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: .equals does not work

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: .equals does not work

    Quote Originally Posted by DarrylBurke View Post
    ... and predictably everyone repeating one another.

    Honestly, which takes more time: signing up to all these forums, or posting an illustrative example of the problem?

    (Sorry for something approaching a hijack, but it's exasperating since this is a possibly interesting question.)
    Last edited by pbrockway2; 01-13-2015 at 04:26 AM.
    gimbal2 likes this.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: .equals does not work

    I'm pretty sure the problem is not in this tiny snippet, this is just the result of tunnel vision. The comparison will succeed - if it is actually reached. I know that because Java works as designed so any problem that would indicate that it does not work as designed is a problem that is happening somewhere else.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: .equals does not work

    Well, the debugger screenshot shows the code just after the comparison. Of course, there are a bunch of if's: if the debugger output is for the code in question, if the watch panel isn't obscuring something important, etc...

    But my guess (just a guess/prediction) is that the problem is right in front of our eyes. The strings aren't the same and the debugger output is merely misleading, not strange or inexplicable.

    Were the OP to respond to the various requests for a simple example of runnable code illustrating the problem my hunch might be confirmed (or at least strengthened) and I might be able to help rather than merely guess.

  8. #8
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: .equals does not work


  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: .equals does not work

    To the OP. Now that your problem is solved, I suggest you go to all the places you asked for help and follow up so folks won't be wasting their time on this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: .equals does not work

    And from the answer there, I would point out that you should be using a proper logging system and not rely on Sysout in a server environment, which could end up going anywhere...or nowhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: .equals does not work

    As I expected, the problem was somewhere else - between the keyboard and the chair.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: .equals does not work

    Quote Originally Posted by gimbal2 View Post
    As I expected, the problem was somewhere else - between the keyboard and the chair.
    Lesson learned: don't allow a cat on your lap while programming.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: .equals does not work

    I don't know, disallowing it usually has far more dire results.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: .equals does not work

    I don't know either; I'm a dog-man; I once had a New Foundlander but that dog was way too large to fit on my lap (and too stupid too ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. using .equals
    By droidus in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 03:33 AM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  3. cant get .equals to work
    By Hooky75 in forum New To Java
    Replies: 14
    Last Post: 05-18-2011, 12:31 PM
  4. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 02:32 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •