• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

learning how to use classes

 
Ranch Hand
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. Hope this isnt a dumb question. I'm writing code to compare two dates and list the difference in months between dates.
works for the year but i keep getting the same answer for the months. example june 1980 and then the date to compare it to may 1982

the answer will be 23 months. but my code would return 24.
then if i enter the same for the first date then January 1984 then it would be 42 but mine will return 48

I commented where i think i went wrong but if anyone can tell me where i went wrong please let me know. its driving me crazy.

 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just scanning through the code, I could spot one potential problem: In the getMonth() method, you are taking String "mon" as an argument, but then you are not using it anywhere in the method, instead you are comparing the class-level "month" variable value with String month names. So, the value "newMonth" obtained in the main method is getting lost in the process, since the months you compare are always the same (i.e. both are values got from class level variable "month"). This is probably the reason why your code only finds difference in the years entered, and then multiplies it by 12 and gives the answer in number of months and you are getting answers like 12 months, 24 months, 48 months etc.

Other than this, in the monthsUtil method, I find the logic to calculate the "diff" a bit weird. For example, if you choose January 2000 and June 2001 as your two dates, then the expression

would give the answer as 7, which is incorrect. I guess you would need to work on this logic a bit.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question is why are you asking for the "day" only once? If you want to compare the dates by considering the day-of-month, then you should also ask for the day value to be compared. Then you would also need to modify the logic for calculating the "diff" (mentioned in my previous reply) by considering the day along with the month and the year values.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and Welcome to Javaranch!
 
Joseph Swager
Ranch Hand
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so far for the answers. Oh and i got rid of day from the program. sorry for the confusion.
Everything works the way i want it but when i need to do the diffrence math on the month.






so lets say when i'm promted for d1 info and i enter June 1980

and now im entering info to compare so i enter July 1981

now the diff should be 13 but its coming back 12.


im passing in the new dates to a method and using it to d1 with




only problem i am having is theMonth and the month always seem to have the same value. Its like the newMonth value im passing is getting d1 value no matter what.

I just want to know why this wouldnt work


 
Joseph Swager
Ranch Hand
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i see the logic problem thank you.
 
Joseph Swager
Ranch Hand
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Pushkar Choudhary,
You where right on the math i changed the - to + and it works

also i was forgetting to add a string var in getMonth ( string month) method. here is the final code and it works like i wanted. thank you thank you.
I'm loving to learn Java.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic