aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compareTo() Method of the String Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compareTo() Method of the String Class" Watch "compareTo() Method of the String Class" New topic
Author

compareTo() Method of the String Class

Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
It is not clear to me as to what the method
" int compareTo(String otherString) " does to a String, in the String Class. It states it does a Lexical Comparison, what is this ???
Does it simply compare the length of 2 strings and return a value less that 0 if the String passed in is shorter in length than the String from which you call the method, and a value greater than 0 if the passed String is greater ???
maybe I dont understand the word Lexical !?!
All Code example greatly appreciated.
Thanks in advance.
waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15
dear but>
Actually a string is less than another if it comes before the other. A string greater than another if it comes ofter the other in dictionary.

REMEMMBER:
int compare(String str)
the invoking string is less than str -- less than zero return
the invoking string is greater than str -- greater than zero return
two string are equal -- zero return
HOPE NOW IT IS CLEAR.
love to all
waseem

love to all<br />waseem akhtar
waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15
dear but>
Actually a string is less than another if it comes before the other. A string greater than another if it comes ofter the other in dictionary.

REMEMMBER:
int compare(String str)
the invoking string is less than str -- less than zero return
the invoking string is greater than str -- greater than zero return
two string are equal -- zero return
HOPE NOW IT IS CLEAR.
love to all
waseem
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi
Thanks Waseem.
The values less than 0 and greater than 0, are they a fixed value or can they vary ??
Thanks, in advance.
waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15
zahid,
values will return is cases:
less than zero
& greater than zero
will very .
BUT MY QUESTION TO ALL READER'S WHO THESE VALUES ARE CALCULATED.
REPLY will be apreciated.
waseem
love to all
waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15
dear zahid,
values will vary. (IN BOTH CASES YOU MENSSIONED)
zero will return only when string are equal.
BUT I DON't KNOW IN WHAT MANNER THE VALUES WILL RETURN IF STRING'S ARE less than OR greater than THE INVOKING STRING.
love to all
waseem

debasish tripathy
Greenhorn

Joined: Jun 22, 2000
Posts: 8
The value returned from compareTo method will depend upon the Strings compared.For example :
class Test{
public static void main(String args[]){
System.out.println(args[0].compareTo(args[1]));}}
Different cases for the different value at the command line arguement
case 1 : "abc" , "abcdef"
The returned value will be the difference of the length of abc and that of abcdef. Output 3-6=-3
Because in this case "abc" is a substring of "abcdef"
case 2 : "abcdef " , "ab"
Output 6-2=4
case 3 : "abhdef" , abdef
Here in this case noone is substring of another.Hence the check is done whether the first 2 letters of the both the strings differ and like wise it goes on till the first ocurring dissimilsrity.In the above case the the 3rd letter of both the strings differ.
Output : The place of h - the place of d =4
case 4 : "abcdef","ad"
Output : b-d=-2
I hope u r clear...


waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15

i am really sorry for dupliction.
Actually when i was submiting reply due to some reason i thought
that you haven't got it .
sorry again.
love to all
waseem
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Thanks Debasish. Your answer was quite clear.
Zahid.
waseem akhtar
Greenhorn

Joined: Feb 24, 2001
Posts: 15
debasish tripathy;
thancks alot . It was very helpfull for me .

love to all
waseem
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
I'm like totally lost on debasish tripathy's explanation for the third and fourth case. Can you elaborate on this?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In Unicode the value of
a=\0061
b=\0062
c=\0063
d=\0064
e=\0065
f=\0066
g=\0067
h=\0068
so when the first unequal character is found the method returns the difference of their unicode values.
For "abhdef" , abdef
h-d = 68 - 64 = 4
b-d = 62 - 64 = -2
using this.charAt(k)-anotherString.charAt(k)
See the unicode values at: http://www.unicode.org/charts/PDF/U0000.pdf
[This message has been edited by Cindy Glass (edited March 19, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Thanks Cindy, you have created a new angle of looking at the topic.
Zahid.
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Thank you Cindy...that was helpful for me also.
 
Consider Paul's rocket mass heater.
 
subject: compareTo() Method of the String Class