This discussion is archived
8 Replies Latest reply: Jan 26, 2008 5:43 AM by 807601 RSS

comparing strings

807601 Newbie
Currently Being Moderated
If String str1 = "Forest" and String str2 = "School", then what is the value of str1.compareTo(str2);?

are they equal?
  • 1. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    compareTo returns an integer. If the two strings are the same, the integer returned is zero. Otherwise, it will be a negative or positive value, depending on the lexicographical order of the strings. I believe the magnitude of the value returned can change, depending on the string values ... that is, there is no guarantee about the specific value returned if the two strings are not equal.

    ? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 2. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    well now I certainly feel stupid. Thank you,
  • 3. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    one more question
  • 4. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    How do I get it to print the value then? those three lines are all that I have. Do i have to declare the value of the comparing as an int?
  • 5. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    public class jk
    {
    public static void main(String [] args)
    {

    String str1 = "Forest";
    String str2 = "School";
    str1.compareTo(str2);
    int compare = str1.compareTo(str2);
    System.out.println(compare);
    }
    }

    It gave me -13...is this correct?
  • 6. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    Yes. Why do you feel stupid?

    Here is some more information:

    The value returned is not important. Given the number of utility methods and classes for dealing with string sorting and comparison, most people don't use compareTo() for string comparisons ... equals() is the more common method for determining whether two strings are lexicographically equivalent. compareTo() is typically used in collection classes for determining inclusion and order. For example, since a TreeSet maintains its value in sorted order, it uses compareTo to during insertion to place the string inserted at the correct location. Or, the contains() method uses compareTo() to determine if the test string is a member of the collection.

    For the interested student, if you read the JavaDoc for the Comparable interface, and pay attention the what is meant for a class to be consistent with equals, you will gain a mathematical understanding of String (or other class) equivalence. Not all classes are consistent with equals.

    ? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 7. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    singramsan89 wrote:
    public class jk
    {
    public static void main(String [] args)
    {

    String str1 = "Forest";
    String str2 = "School";
    str1.compareTo(str2); // <<< THIS LINE IS DOES NOT DO ANYTHING
    int compare = str1.compareTo(str2);
    System.out.println(compare);
    }
    }

    It gave me -13...is this correct?
    I mean, except for that one line, this correctly returns the value of compareTo() for these two strings.

    ? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  • 8. Re: comparing strings
    807601 Newbie
    Currently Being Moderated
    ah, thank you very much sharkura.