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 5 of 5

Thread: Comparing two objects

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing two objects

    I have two objects, player1 and player 2. These two objects have properties: String first name,String last name, double weight and int grade. I want to create a method that takes in a player object and compares it with the players in my list. So, for example, something like this:

      protected void find(T target)
      // Searches list for an occurence of an element e such that
      // e.equals(target). If successful, sets instance variables
      // found to true and location to the array index of e. If
      // not successful, sets found to false.
      {
        location = 0;
        found = false;
     
        while (location < numElements) 
        {
          if ([B]list[location].equals(target)[/B])
          {  
            found = true;
            return;
          }
          else
            location++;
        }
      }

    But when I pass in a player with the EXACT same attributes as a player in the list, the equals method still returns false. What do I have to do to compare two players so that
    player1 = (George, Lemons, 180, 9)
    player2 = (George, Lemons, 180, 9)
    player1.equals(player2)
    returns true?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Comparing two objects

    Does your Player class have an equals method?
    Does your equals method have correct behaviour?
    Improving the world one idiot at a time!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparing two objects

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/52216-comparing-two-objects.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Comparing two objects

    What if I have an equals method in my player class but I want to compare two player objects outside that class? How could I, for example, check to see that an element of an array of type player is equal to some player. i.e.
    protected void find(T target)
      // Searches list for an occurence of an element e such that
      // e.equals(target). If successful, sets instance variables
      // found to true and location to the array index of e. If
      // not successful, sets found to false.
      {
        location = 0;
        found = false;
     
        while (location < numElements) 
        {
          if [B](list[location].equals(target)[/B])
          {  
            found = true;
            return;
          }
          else
            location++;
        }
      }
    How do I use my .equals method here in this separate class to check if they're equal?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Comparing two objects

    Exactly how you have written it. It will only work if your equals (and possibly hashcode) method returns the correct value.
    Improving the world one idiot at a time!

Similar Threads

  1. copying and comparing stacks
    By colerelm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:22 AM
  2. Comparing Two Dates
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 02:09 PM
  3. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM
  4. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM