• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java String comparison dilemma  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below, I was expecting System.out.println would not get executed but it does. If I switch the key and date position in CompareTo code then it won't execute.

Could any one explain to me why it is so?


 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both key and date are Strings. Did you check the API for the compareTo method in the java.lang.String class ? What happens when you compare "02009" to "12008", and "12008" to "02009" ?
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should consider using a (Simple)DateFormat to parse the strings into Date objects, then compare those.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!