File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help Me to Understand Comparisons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 1476
    
    1
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: 1476
    
    1
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: 61606
    
  67

You can edit your posts by clicking the icon.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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: 1476
    
    1
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: 1476
    
    1
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: 24187
    
  34

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: 1476
    
    1
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: 1476
    
    1
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