I know this is fairly simple, but I cannot figure out why I keep getting the out of bounds exception. I have tried initializing the arrays in different ways and that still does not help. I get the exception at the toString method, and at the portion that checks to see if the day is within the correct range of the month its defined for.
public class MyDate
{
//variables for year day and month.
private int year, day, month;
//variable for n which is used to compare to the days for leap years.
int n;
String[] Months = {"January","Feburary","March","April","May","June","July"
,"August" ,"September","October","November","December"};
int[] MaxDays = {31,31,31,30,31,30,31,31,30,31,30,31};
public MyDate(int year, int day, int month)
{
//Checks to see if the year is valid.
if (year < 1600 || year > 3000)
{
System.out.println("The year entered is not valid, "
+ "You entered: " + year);
System.out.println("The year must be between 1600 and"
+ " 3000.");
System.exit(0);
}
//Checks to see if the month is valid.
if (month < 1 || month > 12)
{
System.out.println("The month is not valid, " + "You Entered: "
+ month);
System.out.println("The month must be between 1 and 12.");
System.exit(0);
}
//Checks to see if the day is valid
if (day >= MaxDays[this.day - 1])
{
advance();
}
//Checks for a leap year, and if the day is valid or not.
if ((year % 400 == 0) || (year % 100 != 0))
{
if (year % 4 == 0)
{
if (month == 2)
{
//Loops that goes from 27-31 comparing the day to n.
int n = 27;
do
{
if (n == day)
{
System.out.println("This is a leap year, and you entered " + n + " as the day"
+ "\n" + "this day is not valid for a leap year");
System.out.println();
break;
}
if (n == 32)
{
break;
}
n++;
}
while (n == day);
}
}
}
else
{
this.year=year;
this.day=day;
this.month=month;
}
}
//Checks to see if two dates are equal to eachother.
public boolean equals(Object obj)
{
if (obj instanceof MyDate){
MyDate d = (MyDate)obj;
if (this.getMonth()==d.getMonth() && this.getDay()==d.getDay() &&
this.getYear()==d.getYear())
return true;
else
return false;
}
return false;
}
//gives a general format for the month day and year.
public String toString()
{
return Months[month - 1] + " " + day + "," + year + "";
}