8 Replies - 357 Views - Last Post: 04 February 2012 - 02:10 PM Rate Topic: -----

Topic Sponsor:

#1 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 26-September 11

Checking if Two Objects are Equal

Posted 03 February 2012 - 09:19 AM

I'm having a trouble writing a method that "Compares this Person with another Person object passed in. Comparisons are based on the name and UFID. If the two are equal then the method returns true. false otherwise.

I keep getting these errors:
Person.java:37: cannot find symbol
symbol : method getName()
location: class java.lang.Object
return name.equals((Person)ob.getName()) && UFID.equals((Person)ob.getUFID());
^
Person.java:37: cannot find symbol
symbol : method getUFID()
location: class java.lang.Object
return name.equals((Person)ob.getName()) && UFID.equals((Person)ob.getUFID());
^
Person.java:37: int cannot be dereferenced
return name.equals((Person)ob.getName()) && UFID.equals((Person)ob.getUFID());


I've tried rewriting the method multiple times and I keep getting these errors. Could someone please help? Thank you in advance!

public abstract class Person {
	protected String name;
	protected int UFID;
	protected String dob;
	.
	.
	.
	public boolean equals(Object ob) {
    	        if (ob instanceof Person)
      		         return name.equals((Person)ob.getName()) && UFID.equals((Person)ob.getUFID());
      	        else return false;
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Checking if Two Objects are Equal

#2 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,896
  • Joined: 02-June 10

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 09:26 AM

I'm going to bet this is very close to how C# would make the same complaint about this

Here you are casting ob.getName() to a person
(Person)ob.getName()


((Person)ob).GetName()
Here you are casting ob to a person, then using the .getName() method

This post has been edited by tlhIn`toq: 03 February 2012 - 09:27 AM

Was This Post Helpful? 2
  • +
  • -

#3 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 26-September 11

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:01 AM

Thanks! Ok so I casted ob to a person and used getName and then I got this error. What does it mean that an int cannot be dereferenced?

Person.java:38: int cannot be dereferenced
return name.equals(((Person)ob).getName()) && UFID.equals(((Person)ob).getUFID());
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1780
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:07 AM

That's correct.

(Person)ob.getName()


Here, ob.getName() evaluates to whatever object getName() returns (if that method exists). The cast then applies to the returned object, so the expression as a whole attempts to return a Person object cast from ob.getName(). Fails, as the error output indicates, because Object has no getName() method and you can't cast a String to a Person.

((Person)ob).getName()


Here, the expression begins by casting ob to a Person, then calls getName() on the result, so it returns the String that you wanted. Bear in mind that the cast is not persistent. A call to

ob.getName()



on the next line would fail, because ob is still an Object, not a Person. I point this out because it's a common error in new programmers, pardon me if it's not one you yourself have made.

EDIT: should restrain my blather. This in response to tlhIn`toq

This post has been edited by jon.kiparsky: 03 February 2012 - 10:08 AM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,896
  • Joined: 02-June 10

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:08 AM

Ya got me. I'm not really a java guy. And the code you have here doesn't look like it would produce that.

I'm going to bet it is a problem within the .GetUFID() method.
Can we see that please?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1780
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:11 AM

UFID is an int, which is a primitive type. Primitives are not objects, so they don't have methods. Therefore
UFID.equals(((Person)ob).getUFID()


is illegal. Assuming getUFID() returns an int, you want to use direct comparison here:

UFID == ((Person)ob).getUFID()

Was This Post Helpful? 2
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,896
  • Joined: 02-June 10

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:15 AM

Nice catch Jon. It just never occurred to me that the OP would be making up method calls that don't exist. I skimmed right over that thinking it must in some way be legal in Java.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1780
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Checking if Two Objects are Equal

Posted 03 February 2012 - 10:21 AM

I'm sure the getUFID exists, or I'll assume it does. I'm also pretty sure from the context that it returns an int. The only real problem is the hypercorrection of all == to .equals(). Understandable, since we're forever telling people to use .equals() to compare objects!
Was This Post Helpful? 0
  • +
  • -

#9 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 26-September 11

Re: Checking if Two Objects are Equal

Posted 04 February 2012 - 02:10 PM

Thanks you guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1