wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes compareTo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compareTo" Watch "compareTo" New topic
Author

compareTo

Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
hi,
I have a question,
I'm making a class employer where there are 3 properties:
1) int day;
2)int month;
3)int year;

Now the problem is i want to compare the inputted dates.
My class implements Comparable, I have already written the equals()-method.
But now I am stuck on the compareTo- method...
I want it to rank the date lower if the the dates are equal or lower, and higher if there higher.

I added my code, it's written in dutch though, so I hope it doesn't make things too difficult.
the WerknemersDatum is my own class with the 3 properties and there setters and getters.

Thanks in advance,
Kenneth.
PS as you can see I tried but misarably failed.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Kenneth Van Gysegem wrote:I added my code, it's written in dutch though, so I hope it doesn't make things too difficult.

Not at all (for me atleast... )

Kenneth Van Gysegem wrote:PS as you can see I tried but misarably failed.

Why; what is wrong with the code you wrote and commented out? The logic seems correct, at first sight, except line 69, that should be removed.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
OK,
yeah line 69 was a sort of trie afterwards, luckily i didn't erase all the other code. hehe

The thing is the compareTo gets an errormessage: "returnstatement missing", but I don't see where.
Also isn't this a diffuclt way to do this?

thanks,
Kenneth

PS ben je van Belgie of Nederland?
alleszins aangenaam
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Kenneth Van Gysegem wrote:The thing is the compareTo gets an errormessage: "returnstatement missing", but I don't see where.


The line if(this.datumInDienst.getJaar()==o.getDatumInDienst().getJaar()) (line 78) doesn't have a corresponding else clause. So if it hits that line and the condition is false there's no return value specified.

I'm pretty sure you can simplify it a bit, though. Have a look at java.lang.Integer#compare(int,int), which already does what is needed for a single integer. Use that on the year. If it's non-zero, just return it. Otherwise continue to the month, etc. So you only need two if statements with that.

There's also a slightly cheaty version that relies on you knowing (and enforcing) an upper limit for day and month. Just compare day + 32*(month + 13*year)).
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
Thanks Matthew,

In the end I didn't do it your way but you helped me a lot with the line 78 tip!
Don't know how i could've missed it.

In the end I did it this way

I will however bear your advice in mind, and study the subject a little more.
Thanks again,
Kenneth
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5


However datumInDienst is of the type WerknemersDatum.

Anyway.

If the compiler does not deduce (can not be sure) a value is returned on every execution path from a non-void method, it will complain.

For example this won't compile either:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compareTo
 
Similar Threads
vector sorting
Doubt about Trees Implementations
OO, Equality\Comparable with Composition.
compareTo of Comparable not being called
Treeset adding not based on equals method ?