aspose file tools
The moose likes Beginning Java and the fly likes Help Me to Understand Comparisons Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Help Me to Understand Comparisons" Watch "Help Me to Understand Comparisons" New topic
Author

Help Me to Understand Comparisons

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1254
The following code is always false even though it should be true. The System.out.println displays O Adinistrator

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1254
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

Joined: Jan 10, 2002
Posts: 56214
    
  13

You can edit your posts by clicking the icon.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56214
    
  13

Not sure what this has to do with JSP, but where's the extra "O" coming from?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1254
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1254
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

Joined: Jul 08, 2003
Posts: 24057
    
  13

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?


[Jess in Action][AskingGoodQuestions]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1254
This is my class:

Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
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

Joined: Nov 16, 2004
Posts: 1254
Herman you have saved the day. The trim() made all the difference.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Me to Understand Comparisons
 
Similar Threads
Internal Exception: java.sql.SQLException: Invalid object name
Scope of Session Attributes
Dynamic JSP
please explain with an example
String input with exceptions