Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing strings ...

 
Rohit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
I have a basic JAVA problem..can anyone help on this.
I have a constant declared in a ASMConstanta.java
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
Bartender
Posts: 2936
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Rohit Kumar
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks guys,

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

    if(string1.notEqual(string2){
    } is this a valid one..
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic