Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: What does compareTo method of String do ?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What does compareTo method of String do ?

    Hi all,

    I am just working through all the methods for String in java.

    I am on compareTo at the moment but have to say i dont really know what it does, i have a small working program here to test it:

    package testing;
     
    import java.lang.String;
     
    public class testpage {
    public static void main(String[] args) { 
     
     
        String myString = "abcdef";
        String myStringTwo = "regkaneighuornag";
     
        int myInt = myString.compareTo(myStringTwo);
     
        System.out.println(myInt);
     
    }
    }


    And my result for myInt is: -17

    But what does this -17 actually mean ? Thanks. All java docs say is ''Compares two strings lexicographically."

    But i dont know what this is.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What does compareTo method of String do ?

    The API doc defines what the method returns.
    What definition did google give for: lexicographical

    Did you read the API doc for the method? It is explained there.
    Last edited by Norm; December 8th, 2012 at 04:19 PM. Reason: API doc explains
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What does compareTo method of String do ?

    I was looking at 'Method Summary' I hadnt scrolled Down and seen all the ''Method Detail'' for each method.

    Anyhow, having read it, i understand it compares each index. My issue is why is it returning -17 when the longest string is of length 16.

    I would think it should be returning -16 at most, if no index's match up. ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What does compareTo method of String do ?

    My issue is why is it returning -17
    Reread the API doc. Its explained there.
    Why go past the first character of the two Strings if they are different.
    Execute this:
    System.out.println('a'-'r');  //  compare first two characters
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What does compareTo method of String do ?

    I understand it now, it compares the first one, if equal, moves to the next.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What does compareTo method of String do ?

    Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.

Similar Threads

  1. Problem with string.compareTo();
    By Drasumok in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2012, 04:31 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM