The moose likes Beginning Java and the fly likes comparing strings ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing strings ..." Watch "comparing strings ..." New topic

comparing strings ...

Rohit Kumar
Ranch Hand

Joined: Jul 19, 2007
Posts: 35
I have a basic JAVA problem..can anyone help on this.
I have a constant declared in a
public static String HISTORICAL = "historical";
In another java class..i need to compare 2 strings..
String statusType = req.getParameter("historical"); //this vale comes from JSP hidden parameter and the value of statusType is historical
Now I want to compare with constant vale.. and I am doing like this..
if(statusType.equals( ASMConstants.HISTORICAL)){
statusType = ASMConstants.CURRENT;

Problem is both the values are historical´┐Żbut it is not entering to if condition..any suggestions
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Are you sure both are in the same case. The String equals method is case sensitive so "historical" is not the same as "Historical". If you want a case insensitive equals use the method equalsIgnoreCase(..)

The other thing to check for is that the string you're reading in doesn't contain any leading/trailing white space because " historical" is not the same as "historical". You can remove leading and trailing whitspace using the String classes trim() method.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15100

Are you 100% sure that statusType and ASMConstants.HISTORICAL both contain exactly the same value, "historical"? Because if that's true, then the code inside the block of the if statement will definitely be executed.

Try stepping through the code with a debugger to follow step by step what the program is doing exactly.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Yes, adding to what Tony and Jesper Young told you can have some debugging statements before the if loop.

If your application is enabled with logger utility, you can make use of any of the log() methods or plain old SOP would do. Don't print both the values alone in the statements. As you may not find out if any of the strings contain some whitespaces.

You can do either of the two things given below

  • Try to get the length of both the strings. This way you can make out if there are some extra characters.
  • Try to print some static termination characters before and after the value of Strings as they will help to identify the extra spaces easily if present. (Like "--historical--" is different than that of "-- historical--").

  • If any extra spaces are present, you need to have the trim() method invoked on both the strings to eliminate the confusion.

    If it really does not matter whatever the case both the strings are, you can try making use of equalsIgnoreCase() method.

    Hope this helps out.
    [ August 08, 2007: Message edited by: Raghavan Muthu ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Rohit Kumar
    Ranch Hand

    Joined: Jul 19, 2007
    Posts: 35
    Thanks guys,

    I put trim() and equalsignorecase(). It is working fine now. Also is there any funcationality that performs notEqual.

    } is this a valid one..
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3381

    Simply put a "!" before the condition in the if loop as

    If you really bother about the String references and NOT their contents alone, use != operator as

    I agree. Here's the link:
    subject: comparing strings ...
    It's not a secret anymore!