• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compareTo

 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic