1

I'm getting an out of bounds exception on the "calendarTable[i][j] = str;" line below. I find this funny because I'm initializing it to 15, and I only iterate to 14. I've tried initializing to 20000 and no matter how large of an array I make, it still gets an index out of bounds error.

public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();
        cal.set(2012, 2, 1);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        int dayOfMonth = 1;
        Object[][] calendarTable = new Object[15][15];
        calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
        for (int i = 0; i < 14; i++) {
            for (int j = 0; j < 14; j++) {
                //calendarTable[i][j] = dayOfMonth++;
                if(i%2 == 0){
                    String str = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
                    calendarTable[i][j] = str;
                    cal.add(Calendar.DAY_OF_YEAR, 1);
                }else{
                    calendarTable[i][j] = dayOfMonth;
                }
                calendarTable[i * 2 + 1][j] = "TEST";
            }
        }
        for (int i = 0; i < 14; i++) {
            for (int j = 0; j < 14; j++) {
                System.out.print(calendarTable[i][j]);
            }
            System.out.println("");
        }
    }
1
  • 2
    Did you check the length of calendarTable[0] ? because it is never > 7 Commented Mar 2, 2012 at 17:24

6 Answers 6

3

You have initialized it to 15*15, but then promptly replaced the initial entry with a seven-item array. By the time you get into the iteration, the array at the element zero has only seven items, not fifteen. If you would like to keep it 15*15, copy the names of the days into the array of 15 items.

System.arrayCopy(
    new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}
,   0
,   calendarTable[0]
,   0
,   7);

Also if you would like the index to go full length of the array, use i != 15, i < 15, or i <= 14.

Sign up to request clarification or add additional context in comments.

Comments

2

Because you're running your loop until index < 14

However you have only 7 days in 2nd dimension.

FIX: If you change all occurrences of 14 to 7 then this exception will be fixed however you will have still make sure whether your required output is coming or not.

Comments

1

Because CalendarTable only has 6 indices and it dies when it goes above that value.

if (i % 2 == 0)
        {
            String str = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
            System.out.println("current value of j: " + j);
            calendarTable[i][j] = str;
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }

current value of j: 0
current value of j: 1
current value of j: 2
current value of j: 3
current value of j: 4
current value of j: 5
current value of j: 6
current value of j: 7 //out of bounds

Comments

1

Your problem is here

calendarTable[i * 2 + 1][j]

if i is equals to 8 then 8 * 2 + 1 = 17 giving you the exception

Comments

1

Array size of calendarTable[0]=7 Elements ({"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}) Do you want to assign a value to be written up to 15 items

Comments

1

It's because of the line:

calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};

calendarTable is an array of arrays. Each row does not necessarily need to have the same number of entries as the one above it. Thus, when you actually do calendarTable[0] = new String[], you're telling Java to replace the array at calendarTable[0] with a new one that only has 7 entries, which is why it goes over bounds.

Theoretically, nothing is stopping you from also doing this:

   calendarTable[1] = new String[]{"One Entry"};

It's legal. calendarTable[0].length is 7, and calendarTable[1].length is 1.

I don't know what you're trying to achieve in this program, but you either need to do something like this:

calendarTable[0][0] = "SUNDAY";
calendarTable[0][1] = "MONDAY";
calendarTable[0][2] = "TUESDAY";
calendarTable[0][3] = "WEDNESDAY";
calendarTable[0][4] = "THURSDAY";
calendarTable[0][5] = "FRIDAY";
calendarTable[0][6] = "SATURDAY";

or something like this:

calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "", "", "", "", "", "", "", "", ""};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.