Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Me to Understand Comparisons

 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is always false even though it should be true. The System.out.println displays O Adinistrator

 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mis-spelled should be:

The following code is always false even though it should be true. The System.out.println displays O Administrator
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can edit your posts by clicking the icon.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what this has to do with JSP, but where's the extra "O" coming from?
 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats part of the string that displays in the console:

[5/10/07 10:25:33:543 CDT] 00000033 SystemOut O Administrator

You are right, this is actually servlet code. Sorry.
[ May 10, 2007: Message edited by: Steve Dyke ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with servlets either.

You're comparing a type UserType with a String literal using the 'equals' method of UserType:


Without seeing the implementation for your UserType.equals method nobody on this site will be able to tell you why it's not behaving the way you expect it to.

Moving to Java In General (Beginner) since this is not a Servlet issue.
 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the .equals is mot a method in the UserType class. I just thought this was a built in Java method to use for comparisons.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Dyke:
. I just thought this was a built in Java method to use for comparisons.


It is -- every class has an equals() -- but you have to be using it correctly. Here, if "getUserType()" returns String, then this is OK code, but if it returns any other class, then that's the problem right there. What class does it return?
 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my class:

 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Steve,

It's possible that userText.getUserType() contains spaces at the end, and then your comparison will fail.

To fix that: just add the trim() function
if( userText.getUserType().trim().equals("Administrator")){

Herman
 
Steve Dyke
Ranch Hand
Posts: 1621
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Herman you have saved the day. The trim() made all the difference.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a good time for you to read up on the new Enum type provided by Java 5. This is exactly the type of confusion and ambiguity that they were designed to eliminate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic