1
public  boolean equals(Object o)
{
    boolean result = false;
    if(o!=null && o instanceof Person)
    {

        Person anotherperson = (Person) o;
        if(this.getName() == anotherperson.getName() && this.getCal() == anotherperson.getCal())
        {
            result = true;  
        }

    }
    return result;
}

public int hashCode()
{
    return getName().hashCode() ^ getCal().hashCode();
}

} /////////////////////// what I need to happen is the equal method will compare if the inputed values are identical or not identical, right now it shows not Identical no matter what I change, It seems theres a problem comapring 2 objects in boolean equals.

  • how to compare Strings in java `this.getName() == anotherperson.getName()` see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Scary Wombat Jun 21 '17 at 02:54
  • I think everything you did is correct, except == for string comparison. Just replace == with equals at those places. Day, Month, Year comparison should be fine. However, when comparing date object from Person, use equals instead. In java == is either use to compare primitive variables (like int, boolean etc) or to compare same object reference. Since you are trying to compare two different objects based on their values (and not their references) using equals and hash code, you need to use equals to compare two person objects. – Pramod Jun 21 '17 at 03:02
  • Thanks for the response , I edited it, title seems got a little bit longer . I have limited chance to ask question thats why i put it in one thread. I'm almost done now, I just need to input a value in Person p1 or p2 = new Person (insert value here ) .... i need to insert a value in here like what a scanner does, in commandprompt with a code of java TestPerson / / . .. .. – Jonathan Cruz Jun 21 '17 at 03:15

2 Answers2

0

Strings and MyDate are objects, you need to compare them using equals() instead of ==.

public boolean equals(Object o) {
    boolean result = false;
    if (o instanceof Person) {
        Person other = (Person) o;
        if (this.getName().equals(other.getName())
              && this.getDob().equals(other.getDob())) {
            result = true;
        }

    }
    return result;
}

Note that this equals() implementation does not yet properly address the case in which the name or dob is null.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 1
    The null check is redundant. – shmosel Jun 21 '17 at 02:52
  • Thanks for the response ,I didn't know that now I know better. I'm almost done now, I just need to input a value in Person p1 or p2 = new Person (insert value here ) .... i need to insert a value in here like what a scanner does, in commandprompt with a code of java TestPerson / / . .. .. – Jonathan Cruz Jun 21 '17 at 03:20
  • Is that a new question you're asking in your comment? As far as I can tell, this question is answered, so upvote and/or accept whichever answer(s) helped you solve your problem, and created a new question. – Robby Cornelissen Jun 21 '17 at 03:24
  • @RobbyCornelissen Actually, the OP has already asked this question in the beginning. – Nurjan Jun 21 '17 at 03:40
0

In the class Person change your equals method to this:

public boolean equals(Object o) {
    boolean result = false;
    if (o != null && o instanceof Person) {

        Person anotherperson = (Person) o;
        if (this.getName().equals(anotherperson.getName()) && this.getDob().equals(anotherperson.getDob())) {
            result = true;
        }

    }
    return result;
}

As it has been mentioned, you need to compare String objects using equals and not ==.

EDIT:

You just need to parse the args array in the main method.

Change your class TestPerson to this:

public class TestPerson {

    public static void main(String args[]) {

        String name1 = args[0];
        String[] date1 = args[1].split(", ");
        String name2 = args[2];
        String[] date2 = args[3].split(", ");
        Person p1 = new Person(name1, new MyDate(Integer.valueOf(date1[0]), Integer.valueOf(date1[1]), Integer.valueOf(date1[2])));
        Person p2 = new Person(name2, new MyDate(Integer.valueOf(date2[0]), Integer.valueOf(date2[1]), Integer.valueOf(date2[2])));

        if (p1.equals(p2)) {
            System.out.println("Identical");
            System.out.println(p1);
            System.out.println(p2);
        } else {
            System.out.println("Not Identical");
            System.out.println(p1);
            System.out.println(p2);
        }
    }
}

Note that the input should be in quotes. For example, "Jose", "1, 1, 2000". I make an assumption that your date format is exactly as in my example above.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • Thanks for the response ,I didn't know that now I know better. I'm almost done now, I just need to input a value in Person p1 or p2 = new Person (insert value here ) .... i need to insert a value in here like what a scanner does, in commandprompt with a code of java TestPerson / / . .. .. – Jonathan Cruz Jun 21 '17 at 03:20
  • @JonathanCruz I edited the answer. It should work now. – Nurjan Jun 21 '17 at 03:41