0

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.

    public static int dayNumber(int day, int month, int year){  
      int daysInMonth = 0;  
      int days = 0;  
      boolean leapYear = isLeapYear(year);  
        for(int i = 1; i <= month; i++){  
            switch(month){  
                case 1: daysInMonth += 31;  
                case 2: if(leapYear)  
                     daysInMonth += 29;  
                    else  
                     daysInMonth += 28;  
                case 3: daysInMonth += 31;  
                case 4: daysInMonth += 30;  
                case 5: daysInMonth += 31;  
                case 6: daysInMonth += 30;  
                case 7: daysInMonth += 31;  
                case 8: daysInMonth += 31;  
                case 9: daysInMonth += 30;  
                case 10: daysInMonth += 31;  
                case 11: daysInMonth += 30;  
                case 12: daysInMonth += 31;  
                break;  
            default:  
            break;  
         }  
         while(month <= 12){  
            days += daysInMonth + day;  
            month++;  
         }  
        }  
        return days;  
    }  
  • You are missing the break after each case, but if you don't need to implement this (say, for homework), there is a java function that does this very well: http://stackoverflow.com/questions/12525396/today-is-nth-day-of-year – luanjot Nov 01 '13 at 20:56
  • Can you use `Calendar.get(Calendar.DAY_OF_YEAR)`? That would save you an awful lot of code. – Jim Garrison Nov 01 '13 at 20:56
  • its a class project where I need to create my own methods, I tried with and with out the breaks, and it came back with incorrect values – Irving Luna Nov 01 '13 at 20:58

2 Answers2

3

You need to terminate each case with a break:

            case 1: daysInMonth += 31;  
                    break;
            case 2: if(leapYear)  
                      daysInMonth += 29;  
                    else  
                      daysInMonth += 28;  
                    break;
            case 3: daysInMonth += 31;  
                    break;

and so on.

Without this, statements in a switch fall through.

Additionally, your loop variable is i and you switch on month (but then modify month in another, nested, loop).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • This "requiring break" can be desirable because it allows you to omit it when you want case 1: do x,y and z, case 2: do y and z, case 3: do z – Richard Tingle Nov 01 '13 at 20:55
  • @RichardTingle: Yes, but how does that relate to the question we are answering? – NPE Nov 01 '13 at 20:56
  • It answers the next logical question "why would java behave like that?!", seemed useful information but not worthy of an answer, I take it you disagree – Richard Tingle Nov 01 '13 at 20:59
  • @RichardTingle: Oh, I think the answer to *that* question is "because C does". ;) – NPE Nov 01 '13 at 21:00
  • thanks for the help, i've tried adding the break after each case and it fails, i've also changed month to i, but the programs just stalls, im not really undestanding what you mean. – Irving Luna Nov 01 '13 at 21:03
0

There is really no need for a loop here... This should do the trick:

days = day
days += (month-1)*30;
days += (month)/2; // every odd month until July has 31 days
days += (month >= 8 && month % 2 == 1) ? 1 : 0;   // so if we have august or later and in an even month, add 1 day
if (month > 2) {
    days -= (isLeapYear(year)) ? 1 : 2;
}

Just read your comment about this being exercise for school, so I'll better explain my code a little bit more.

First, we assume every month just has 30 days.

This, of course, is incorrect - every second month, starting with January, has 31. SO we're calculating how many month with 31 days we had so far. As it's the odd month (at least until August) that have the 31 days, we're dividing the month by two (remember - integer division, we'll get floor(month/2)) to get the number of month that have passed and had 31 days.

This is still incorrect, as starting with August, we have another day to add - our previous calculation yields one month with 31 days less than we really had. So we just add that one day if an even number of month has passed (we can tell this by dividing month by two and looking at the leftover, this is called "modulo division" and written "month % 2").

Finally, we're going for February. If February has passed (=we are in march or later) we just subtract two days - or one if it's a leap year. I used a so called "ternary statement" here (the ... ? ... : ... thing). Basically it's short for if (isLeapYear(year)) { days -= 1; } else { days -= 2; }

Johannes H.
  • 5,875
  • 1
  • 20
  • 40
  • great thank you makes a lot of sense, the methods working correctly now. – Irving Luna Nov 01 '13 at 21:21
  • Edited my code - it wasn't working for even month > august. Please make sure to update your code accordingly. Should be working now - can somebody else please verify? – Johannes H. Nov 01 '13 at 21:27
  • it was working before, made small changes, an it works but for some reason it returns incorrect values for two of the dates on my list, this is what I have, subtracted 1 from first month because from what I read, you already had one of the months with days = day, correct? – Irving Luna Nov 01 '13 at 21:54
  • public static int dayNumber(int day, int month, int year){ int daysInMonth = 0; int days = day; boolean leapYear = isLeapYear(year); days += (month-1)*30; days += (month+1)/2; // every odd month until July has 31 days days += (month > 8 && month % 2 == 0) ? 0 : 1; // so if we have august or later, add 1 day days -= isLeapYear(year) ? 1 : 2; return days; } – Irving Luna Nov 01 '13 at 21:55
  • Can you just give me the dates that are not working with my code? I haven't done too much testing on it, so there might still be errors I missed. – Johannes H. Nov 01 '13 at 21:59
  • 4-Jan-1776 is day 188 in 1776 31-Jan-2001 is day 92 in 2001 – Irving Luna Nov 01 '13 at 22:01
  • public static boolean isLeapYear(int year){ boolean isLeapYear = false; if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) isLeapYear = true; else isLeapYear = false; return isLeapYear; } – Irving Luna Nov 01 '13 at 22:02
  • Fixed my code above. I missed that, of course, the month we're in has not yet passed. Did extensive testing on it, it should work OK now. – Johannes H. Nov 01 '13 at 22:21
  • yes, mostdeff, it works with out any issues now, thnks for the help – Irving Luna Nov 01 '13 at 22:28