| 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: 16815
|
|
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: 26496
|
|
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: 24081
|
|
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: 16815
|
|
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:
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: 19232
|
|
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
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: 16815
|
|
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
|
 |
 |
|
|
subject: Equals Method problem
|
|
|