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;
}
}