• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding days in a year  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.

 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Calendar can do this already for you, you don't need to program it yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!