• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

compareTo

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic