Results 1 to 8 of 8
  1. #1
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Comparator problems

    Hi everyone,

    Having a problem in the fact that i have created a Comparator interface and a PersonComparator class. When I try to place this method in another of my classes

    public void averages (Player a, Player b)
    {
    PersonComparator.compare (a, b);
    }

    i get an error non-static method compare(Player,Player) cannot be referenced from a static context.

    As far as I can tell I'm not trying to reference it from a static context...

    Any help is much appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Comparator problems

    You are, since compare() is a non-static method and you do not have a PersonComparator object to call it on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Comparator problems

    Ok, can you expand on that, so I know what I have to do.

    Im very new to Java so as basic as possible explanation would be great..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Comparator problems

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Comparator problems

    PersonComparator.compare
    That is the syntax for calling a static method: Classname.methodName(). For example: Math.random()
    Change the symbol: PersonComparator to a variable that references the class with the compare method.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Comparator problems

    I have went from my initial problems to the fact that when i try to call the following in my main method.

    Collections.sort (Club.england, new PlayerComparator());

    i get this error

    cannot find symbol - method sort(java.util.ArrayList<Person>,PlayerComparator) ; maybe you meant: sort(List<T>) or sort(List<T>, Comparator<? super T>)

    can someone please help..

    Ive been trying to solve this for hours...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Comparator problems

    Your Comparator needs to match the type used for the List, so your PlayerComparator would need to be:
    Java Code:
    public class PlayerComparator implements Comparator<Person> {
    etc etc
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Comparable and Comparator
    By jeanjiang in forum New To Java
    Replies: 7
    Last Post: 04-23-2011, 08:59 AM
  2. sorting by Comparator
    By Dayanand in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 11:38 AM
  3. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 11:25 AM
  4. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 03:46 PM
  5. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:38 PM

Posting Permissions

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