File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Equals Method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals Method problem" Watch "Equals Method problem" New topic
Author

Equals Method problem

Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Hello..... please help ... I'm

I'm trying to compare two integers and return the value to another class, and I keep getting false even though the values are equal:

here is the method:


if you need the whole probram... here you go...


Any help you can provide is greatly appreciated... I should have a concussion from beating my head against this wall for so long... *sigh*
Tiffanhy
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18118
    
  39

Hint: Are you sure that you are calling your equals() method? Or the one the you inherited from the Object class?

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Henry Wong wrote:Hint: Are you sure that you are calling your equals() method? Or the one the you inherited from the Object class?


Thanks for replying Henry. I'm still lost... I'm super noob. I'm in Ch. 4 of a very bad text book with assignments that are less than 1/2 explained. I'm still not sure why it's returning false. I tried comparing the values both within the main method and in my Counter class... but no dice.

What am I doing wrong?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

Tiffany,
Welcome to JavaRanch!

Take a look at your equals() method. What does it take as a parameter? What are you passing? (it isn't the same type.)

If you don't know the answer to either of these questions, post which one you do know. Or you can see for yourself by renaming your method from "equals" to "equalsValue". This will give you a compiler error rather than functioning in a way to don't expect. And the compiler will answer the questions too.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Jeanne Boyarsky wrote: Or you can see for yourself by renaming your method from "equals" to "equalsValue".


This is a good idea, but note that you have to change the name in two places for this to work. You have to change it where you define the function, and then also change it where you call the function, so i.e.,

... counter.equalsValue(counter2));


[Jess in Action][AskingGoodQuestions]
Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Jeanne Boyarsky wrote:Tiffany,
Welcome to JavaRanch!

Take a look at your equals() method. What does it take as a parameter? What are you passing? (it isn't the same type.)

If you don't know the answer to either of these questions, post which one you do know. Or you can see for yourself by renaming your method from "equals" to "equalsValue". This will give you a compiler error rather than functioning in a way to don't expect. And the compiler will answer the questions too.


Thanks for the welcome.
I'm actually not passing anything anymore... I deleted the method in the counter class and am just using the following code in the main program:


I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...

Here's my code:
Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Actually... I guess I was wrong about deleting the equals method... my homework assignment specifically calls for one... this just might kill me. Where's my nouse...

please help.... I'm lost and I can't find my way out...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18118
    
  39

Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...


Just because you are not getting a compiler error doesn't mean that it is correct.

Your equals() method takes an primative int. You are passing it a Counter object. Obviously, these two types are completely incompatible... Something is obviously wrong.

What is wrong is... it is not calling your equals() method, but is calling the equals() method that is inherited from the Object class.... as already mentioned.

Henry
Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Henry Wong wrote:
Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...


Just because you are not getting a compiler error doesn't mean that it is correct.

Your equals() method takes an primative int. You are passing it a Counter object. Obviously, these two types are completely incompatible... Something is obviously wrong.

What is wrong is... it is not calling your equals() method, but is calling the equals() method that is inherited from the Object class.... as already mentioned.

Henry


Okay... that makes sense. Is there a way to make it call my equals method? I guess having two methods with the same name messes things up...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Does your class have an equals method with exactly the same parameter types as Object.equals? If not then you're not overriding that method but overloading it instead.

Note that an equals method should not throw any exceptions if null is passed, or if the argument is of a completely different class. It should return if that is the case.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tiffany Smith
Greenhorn

Joined: Feb 13, 2010
Posts: 6
Rob Prime wrote:Does your class have an equals method with exactly the same parameter types as Object.equals? If not then you're not overriding that method but overloading it instead.

Note that an equals method should not throw any exceptions if null is passed, or if the argument is of a completely different class. It should return if that is the case.



Thanks everyone who replied... I just dropped the class. I am going to try this again with a better textbook/better teacher/and hopefully it will make sense.
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
From stuck on homework to "dropped the class" in less than 4 hours. Whew, what a trooper!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18118
    
  39

Bert Wilkinson wrote:From stuck on homework to "dropped the class" in less than 4 hours. Whew, what a trooper!


Well, that is one of the purposes of school. To find what you like. To find what you don't like. To find what you are good at. To find what you are not good at. Better to figure it out now, before you enter the work force.

Henry
Johnny Doe
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Hi all, I'm reviving this thread because I'm stuck at the same point - same point of not understanding. And same homework problem. I understand that I can create a method an invoke it by using the object then a dot then the method. I don't get this equal overiding thing.
I think the book must bury this info somewhere.
Anyone want to demonstrate it (not the homework but rather the idea) with a few lines of code I can run (but not too few)? Maybe comment it? Thanks.
And you have only 4 hours or else..... :}

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18118
    
  39

Johnny Doe wrote:Hi all, I'm reviving this thread because I'm stuck at the same point - same point of not understanding. And same homework problem. I understand that I can create a method an invoke it by using the object then a dot then the method. I don't get this equal overiding thing.
I think the book must bury this info somewhere.
Anyone want to demonstrate it (not the homework but rather the idea) with a few lines of code I can run (but not too few)? Maybe comment it? Thanks.
And you have only 4 hours or else..... :}


To answer Tiffany issue, not that it would matter as she dropped the course three years ago, she did *not* override the equals() method. To override a method, she needed to have the same signature -- she didn't. She had a different parameter, and hence, was calling the equals() method, from the Object class, that she did not override.


Now, Johnny, for you to be at the same point, you would have to make the same mistake. Is this true? And BTW, welcome to the ranch.

Henry
Johnny Doe
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Hi thanks for the help. I made the same error.

In this homework there is the following "demo" that we *must* use. But I wonder : at the place below where I wrote 'huh?' (in the demo) is there a logic error?
Shouldn't they be comparing the value that is being incremented rather than the counter and counter2 objects themselves?




Johnny Doe
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Okay - this seems to work. I used your hint that suggested that to override the equals method I must use the same (same parameter) [an object - in this case] in both the invocation (?) and the overriding method (?).

I guess, that when I do that- it works. I didn't really know that I could send a Counter object into the method. But I guess that is what alerts the compiler to do an override (?)
But what is the point of all that? Maybe I should do a non-override usage somehow? Compare two ints?


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Johnny Doe wrote:I guess, that when I do that- it works. I didn't really know that I could send a Counter object into the method.

Not only can you; you MUST send an object to an equals() method because that's what they're for - comparing OBJECTS.

But I guess that is what alerts the compiler to do an override (?)

The signature of the method - which is STILL wrong.

Look carefully at the signature for Object.equals(), and copy/paste it if necessary.

Also, adding an "@Override" annotation will make the compiler give you an error message if you get it wrong.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals Method problem
 
Similar Threads
Necessity to override both equals() and override() methods
Create a counter that increments once a second until a given number of seconds is reached
Word Counter
Why isn't @Ignore working in this code?
Synchronizing thread woes!