-2

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 + "";
} 
  • 3
    Have you run this in a debugger? That is usually the easiest way to find these things – Cort Ammon Sep 12 '13 at 02:51
  • Please post the entire text of the exception and mark which line of your code it complains about. – PM 77-1 Sep 12 '13 at 02:53
  • change MaxDays[this.day-1] to MaxDays[month-1]; – grape_mao Sep 12 '13 at 02:55
  • Looks like you have many errors here. I think this is another one: In `toString()`, this would be failing `Months[month - 1]`. Reason: `this.month` defaults to 0. You pass and validate `month` in your constructor, but you only ever set `this.month=month` if this expression is false `if ((year % 400 == 0) || (year % 100 != 0))`. So you end up trying to access `Months[-1]`. – sbk Sep 12 '13 at 02:58
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at MyDate.toString(MyDate.java:120) at java.lang.String.valueOf(Unknown Source) at java.io.PrintStream.println(Unknown Source) at MyDate.MyDateDemo(MyDate.java:145) at MyDate.main(MyDate.java:167) this was the whole exception, I fixed the one with the maxdays, but the other tostring method is still failing. – Marcus Scaffidi Sep 12 '13 at 03:05
  • and yes i have run it in the debugger, I still cant figure out why its doing it. – Marcus Scaffidi Sep 12 '13 at 03:07
  • @Marcus Scaffidi See my update. – Alex Sep 12 '13 at 03:09

2 Answers2

1

It seems the problem is coming from there

if (day >= MaxDays[this.day - 1])

where this.day = 0

I suppose you should pass month instead.

if (day >= MaxDays[month - 1])

UPDATE
In your toString() method month = 0 because you did not set month before.
Do this.month=month; outside of else block in the constructor.

Alex
  • 11,451
  • 6
  • 37
  • 52
  • Thanks alot!! My whole problem was having the this.year=year; and so on in the else statement, and it wasnt setting the correct values, they were just setting to 0 automatically. – Marcus Scaffidi Sep 12 '13 at 03:14
0

Probably the error is here, in your if condition

 if (day >= MaxDays[this.day - 1])

MaxDays[] is an array with 12 elements - index 0 - 11

days can be from 1-31, so if day is greater than 12, it will give an IndexOutOfBoundException.

In your code though it would be always zero since you are not initializing it before using it.

JNL
  • 4,683
  • 18
  • 29