This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding compareTo

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to get rid of this error and what causes it?
thanks
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to get rid of this error and what causes it?

What error?? Can you post the error that you are getting...
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kevin,


One hint for you. Do you think that compareTo() method is defined for integers?
What you are trying to do is year.compareTo(), and nothing like compareTo exists
for integers.


Do you think, it might help you?
Hope this helps you,
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# int cannot be dereferenced
# return year.compareTo(b.year);


year is primitive data type so it do not have compareTo method

Dejan
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Prithivi

using a wrapper class is the solution then.is it?
thanks
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dejan Miler wrote:
# int cannot be dereferenced
# return year.compareTo(b.year);


year is primitive data type so it do not have compareTo method

Dejan

thanks
 
Moguluri Ravi Kiran
Ranch Hand
Posts: 63
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kevin Lee,

I dont know the way you want your Book class to work....
but i am telling the way how to use the compareTo()

& your error is simple , all our friends already explained.

you need here two things

1) how to use equals()
2) intention of compareTo()

for equals() go through Thinking in Java ed.4...

now for the CompareTo() the implementation must be as follows.

let b1 & b2 are your book instances

1) if b1.equals(b2) is true then b1.compareTo(b2)==0 must be true...
2) if b1.equals(b2) is false then b1.compareTo(b2) must return a non-zero...
3) any_book_obj.compareTo(null) must throw NullPointerException.
4) for incompatiable class it must throw ClassCastException.


Click Here for More

your modified program is

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you ravi
I wanted to sort the Book objects according to the year.Thats why I used compareTo in Book claas.After making year an Integer instead of a primitive the code worked.
Is there any other means of getting it done that is better?(to be used with sort())
thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic