• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing strings ...

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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

     
    reply
      Bookmark Topic Watch Topic
    • New Topic