0

I have a problem regarding index out of bound error in arrays. I want to create a method that takes month and day as parameter and returns day of the year.If any of the parameters are incorrect, the method should return 0.

for example if the method receives 2 and 3, meaning the third of February it must return 34. If it receives 12 and 31, it must return 365. but I am getting this problem of index out of bound and can't solv it any tips. this is my code.

public class CalendarMethods {

public static int dayInYear(int month, int day){

int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    int yearCalculation = 0;
    int monthCalculation = 0;
    

    
    for (int j = 0; j < month-1; j++) {
        monthCalculation = monthCalculation + daysInMonth[j];
    
    }
    
    yearCalculation = monthCalculation + day;
    if (month <= 0 ) {
        yearCalculation = 0;
    }
    if (month >= 13){
        yearCalculation = 0;
    }
    return yearCalculation;
    
}

}

  • your if(month >= 13) gets called after the for loop. Hence if you call your function with month = 14 (for instance), it will get out of the bounds of the array as j goes up to 13 > 11 and your array only has 12 elements. To prevent that you should either do an early return or an if else statement – laenNoCode Dec 30 '21 at 13:21

2 Answers2

0

You should do the boundary checks on month before the loop. A fix for that would be:

    public class CalendarMethods {
    
    public static int dayInYear(int month, int day){
    
        int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        int yearCalculation = 0;
        int monthCalculation = 0;

        if (month > 0 && month < 13) {
           if (day > 0 && day <= daysInMonth[month - 1]) { // extra: check if entered day makes sense
             for (int j = 0; j < month-1; j++) {
               monthCalculation = monthCalculation + daysInMonth[j];
             }
        
             yearCalculation = monthCalculation + day;
          }
       }

        return yearCalculation;
    }
}
Yahli Gitzi
  • 395
  • 1
  • 3
  • 17
0

For functions, always do sanity checks first to ensure the inputs you are getting align with your expections, in this this case we expect months to be values between 1 and 12 so same implementation as you had but check inputs before processing the rest of the code. This saves you from exceptions from unexpected/bad inputs.

   public static int dayInYear(int month, int day){


        if (month <= 0||month >= 13 ) {
           return 0;
        }
   
    
        int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        
        int yearCalculation = 0;
        int monthCalculation = 0;
        
    
        
        for (int j = 0; j < month-1; j++) {
            monthCalculation = monthCalculation + daysInMonth[j];
        
        }
        
        yearCalculation = monthCalculation + day;
       
        return yearCalculation;
        
    }
Daniel Kiptoon
  • 314
  • 1
  • 7