Results 1 to 11 of 11

Thread: Please Help me

  1. #1
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default Please Help me

    Hi,

    I am new and working in java. I would like to know how .compareTo() works when we compare two string values. Which means for example, consider two string values:

    String value1="24.0";
    String value2 ="250.0";

    int valResult = value1.comapreTo(value2);

    which returns output as -6.

    So can anyone explain me.

    Thanks,
    Ajitha

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ajitha View Post
    Hi,

    I am new and working in java. I would like to know how .compareTo() works when we compare two string values. Which means for example, consider two string values:

    String value1="24.0";
    String value2 ="250.0";

    int valResult = value1.comapreTo(value2);

    which returns output as -6.

    So can anyone explain me.
    That's strange because my compareTo( ... ) method (I checked the source code) returns for two unequal characters c1 and c2 the value c1-c2 which happens to be -1 here. Is the above your actual code?

    kind regards,

    Jos

  3. #3
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi Josah,

    Above code is my actual code.


    Thanks,
    Ajitha

  4. #4
    ujjal.ruet is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Ajitha,
    My code also results -1.You check your code.Yow wrote wrong spelling.See below...
    You wrote...
    value1.comapreTo(value2);

    Actually it will be......
    value1.compareTo(value2);


    Is it clear?
    If it then please click the REP button.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ajitha View Post
    Above code is my actual code.
    Strange ... what do you get when you compare, say, "A" and "B", and "A" and "C", and "A" and "D"? The result should be -1, -2 and -3 respectively if you're using Sun's JVM version 1.6.

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    From the compareTo api you can read


    compareTo
    public int compareTo(String anotherString)Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
    This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

    this.charAt(k)-anotherString.charAt(k)
    If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
    this.length()-anotherString.length()

    Specified by:
    compareTo in interface Comparable<String>
    Parameters:
    anotherString - the String to be compared.
    Returns:
    the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

  7. #7
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi all,

    Thanks for your reply. Actually in my code I have used 75.0 instead of 250.0, so that I got -6 value. Sorry for wasting all of your precious time.

    And first I don't know how compareTo() works. I read the post and came to know about it well.


    Thanks for all again....

    Regards,
    Ajitha

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ajitha View Post
    Thanks for your reply. Actually in my code I have used 75.0 instead of 250.0, so that I got -6 value. Sorry for wasting all of your precious time.
    That is strange too because the return value of the compareTo( ... ) method should've been 5 in this case (If we're still talking about String values here).

    kind regards,

    Jos
    Last edited by JosAH; 06-04-2010 at 04:24 PM.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    why 5? is it binary or something?

  10. #10
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi Josah,

    I got -5 as output if I compare 24 with 75.

    (I) String value3 = "24.0";
    String value5 ="75.0";
    int Result=value3.compareTo(value5);

    But, when i tried with below values and got 5 as output.

    (II) String value3 ="75.0";
    String value5="24.0";
    int Result = value3.compareTo(value5);

    I need to compare using (I) option only.

    Thanks,
    Ajitha

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ajitha View Post
    Hi Josah,

    I got -5 as output if I compare 24 with 75.

    (I) String value3 = "24.0";
    String value5 ="75.0";
    int Result=value3.compareTo(value5);
    That is correct; the comparison method checks/compares all character from
    left to right; when it finds two diferent characters c1 and c2 it returns c1-c2; here in your example it will return '2'-'7' which happens to be equal to -5.

    [QUOTE=Ajitha;124677]
    But, when i tried with below values and got 5 as output.

    (II) String value3 ="75.0";
    String value5="24.0";
    int Result = value3.compareTo(value5);
    [QUOTE=Ajitha;124677]

    That also makes sense: '7' and '2' are different so '7'-'2 is returned which equals 5. (compare this with your previous example).

    kind regards,

    Jos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •