-2

So it seems that my java program is not able to recognize two identical objects even though I'm using the same method as in a previous question where java did recognize the two objects as being the same. The only difference in this program is I have an extra keyboard.nextLine() which is necessary for inputting the name/address of object two.

Here's the class input

    public boolean equals(Person num1) {
    if ((this.name==num1.name)&&(this.address==num1.address)&&
    (this.age==num1.age)&&(this.phoneNumber==num1.phoneNumber))
        return true;
    else
        return false;
}

Here's the demo input

   import java.util.Scanner;
   public class PersonDemo {

   public static void main(String[] args) {

    Person num1, num2;
    num1=new Person();
    num2=new Person();

    String name, address;
    int age; 
    long phoneNumber;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Input the name of person 1:");
    name=keyboard.nextLine();
    System.out.println("Input the address of person 1:");
    address=keyboard.nextLine();
    System.out.println("Input the age of person 1:");
    age=keyboard.nextInt();
    System.out.println("Input the phone number of person 1:");
    phoneNumber=keyboard.nextLong();
    keyboard.nextLine();

    num1.setName(name);
    num1.setAddress(address);
    num1.setAge(age);
    num1.setPhoneNumber(phoneNumber);

    System.out.println("\nInformation of person 1: ");
    System.out.println(num1);

    System.out.println("\nInput the name of person 2:");
    name=keyboard.nextLine();
    System.out.println("Input the address of person 2:");
    address=keyboard.nextLine();
    System.out.println("Input the age of person 2:");
    age=keyboard.nextInt();
    System.out.println("Input the phone number of person 2:");
    phoneNumber=keyboard.nextLong();

    num2.setName(name);
    num2.setAddress(address);
    num2.setAge(age);
    num2.setPhoneNumber(phoneNumber);

    System.out.println("\nInformation of person 2: ");
    System.out.println(num2);

    if (num1.equals(num2))
        System.out.println("\nPerson 1 and person 2 are identical.");
    else 
        System.out.println("\nPerson 1 and person 2 are not identical.");
}


   } 
Froblinkin
  • 374
  • 3
  • 14

2 Answers2

1

For Object(such as String), you need to use equals method. == only tests if these two Objects are the same objects (compare the memory address). In your case, num1.name and num2.name are different objects although the contents of them are the same.

kosa
  • 65,990
  • 13
  • 130
  • 167
zsxwing
  • 20,270
  • 4
  • 37
  • 59
0

Problems:

  • not overriding Object.equals method correctly
  • inside equals method, testing equality using ==, instead of equals
  • not testing num1 for null

Do this:

@Overrides
public boolean equals(Object num1) {
    boolean result = false;
    if (num1 != null && num1 instanceof Person) {
        Person personNum1 = (Person)num1;
        result = this.name.equals(personNum1.name) &&
                 this.address.equals(personNum1.address) &&
                 this.age.equals(personNum1.age) && 
                 this.phoneNumber.equals(personNum1.phoneNumber);
    }
    return result;
}

BTW, any reasonable tutorial on equals would show this. :)

Glen Best
  • 22,769
  • 3
  • 58
  • 74