• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding compareTo

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How to get rid of this error and what causes it?
thanks
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic