• 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

adding days in a year

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to create a method for a program that accepts the date and if valid will add all the days from 01/01/xx to the date in the that year, eg 10/1/1999 will display "day 274 in 1999". I have the following code below, but its not adding the correct values. Not sure what I am doing wrong.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Irving,

first of all: contrary to every BASIC I know of, you must use "break" for each "case" statement,
otherwise things go horribly wrong.

Second: suppose you have a date in January, i.e. month = 1;
do you think it is right to set 'daysInMonth" to 31 (in your switch routine)?

Third: see line 06. Do you think you are using the correct variable here?

Lastly, what is the idea behind the "while" loop in line 26?

I suggest you put some System.out.println(variable) at handy places to see how your variables develop.

Greetings,
Piet

Edit: I just notice that the while-loop in line 26 is part of the for-loop. I thought is was outside.
Nevermind, just use the println() advice and you'll see what is happening.
 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an easier method to do this, i'll have to come and review case-switches, thanks for the println adv though, ill be sure to use this to find where the bugs are coming from in the future.
 
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
java.util.Calendar can do this already for you, you don't need to program it yourself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic