Forums Register Login
What does ArrayIndexOutOfBoundsException mean and how do I get rid of it?
My program compiles correctly but I get the same message when I try and run it:

package displaycalendars;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DisplayCalendars {

 public static void main(String[] args){
   Calendar calendar = new GregorianCalendar


 public static void printHeader(Calendar calendar){

   int month = calendar.get(Calendar.MONTH)+1;

   String monthName = "";

     case 1: monthName="January";
     case 2: monthName="February";
     case 3: monthName="March";
     case 4: monthName="April";
     case 5: monthName="May";
     case 6: monthName="June";
     case 7: monthName="July";
     case 8: monthName="August";
     case 9: monthName="September";
     case 10: monthName="October";
     case 11: monthName="November";
     case 12: monthName="December";

   System.out.println(" "+monthName+ ", " +calendar.get(Calendar.YEAR));
   System.out.println("Sun Mon Tue Wed Thu Fri Sat");

 public static void printData(Calendar calendar){
   int dayinWeek=calendar.get(Calendar.DAY_OF_WEEK);

   for (int i = 1; i< (dayinWeek); i++)
     System.out.print(" ");
   for (int i = 1; i <
       (Calendar.DATE); i++){
Hi David, welcome to the Ranch!

The first thing you need to know is, what line of your code threw that exception?

When you ran the code, you got a lot of information of which the top line told you what kind of exception happened, I suppose. That's called a stack trace and it tells you more than just the name of the exception. It should tell you the line number of the place the exception happened. If you can't work out where that is, then go ahead and post the stack trace here. (You should be able to copy and paste it, don't try to retype the whole thing.)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at displaycalendar.DisplayCalendars.main(DisplayCalendars.java:9)
C:\Users\dalcorn\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

David Alcorn wrote: at displaycalendar.DisplayCalendars.main(DisplayCalendars.java:9)

So, this is the relevant line. It says the exception happened in your DisplayCalendars class, in the main() method, at line number 9. I think I can tell which line that is, but it isn't quite obvious from the way you posted the code. But the code editor you're using should tell us which is line 9.

When you find that line, you'll find that you're trying to access an array entry and you're trying to access an entry which is outside the bounds of the actual array. (That's what "ArrayIndexOutOfBoundsException" means.) And the exception tells you which array entry you tried to access:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

You tried to access array entry number 1, and that's outside the array. So... what does that tell you?
Welcome to the Ranch (again).

Have you worked out what Paul means about which array you are trying to get the 2nd element of?
Let's copy your code, but inside code tags:-Doesn't that look better, but the line Paul was thinking of now appears to be number 10.

Have you been told to use Calendar and GregorianCalendar? They are dreadful to use. Since the introduction of new date classes in 2014, most of us have been only to happy to stop using Calendar.
Have you been told to use a switch? I can think of much easier ways to print a month: if you look through the Java™ Tutorials you can probably find about the %T tag to use with printf (I am pleased to see you know how printf works): try this section and this one and this one about new date classes. I don't know how relevant this section is. If you still haven't found out about the %T tag, go through the Formatter documentation, which shouldn't take more than a week to read.
If you still need to use a switch, use the constants from the Calendar class, which will get you out of needing some of the strange arithmetic for the strange month numbers (another reason for not using Calendar).I am not quite sure I understand the loop. What is it supposed to do?
Don't write System.out.println("\n"); or similar. Write System.out.println(); instead. If you need a new line whilst using printf be sure to write %n.
It is a class assignment and that is what's required.
Did you remember to specify program arguments when you ran your program? Think about what happens when you don't.
OK. I set up the argument(correctly, I think but I am getting the following output...
[Thumbnail for Capture.PNG]
That's because this code:

Doesn't do what the requirements need your program to do. The loop expression on line 63 is not correct. Why i < (Calendar.DATE)? The value of Calendar.DATE is constant. It never changes. I think you meant to pass that to a method call on your calendar object. Also, your for-loop on line 62 prints a new line on every iteration.  I think you only want to print a new line if you get to a Saturday, right?

Since you know about methods, then you should realize that methods are a great way to break up a big, complicated task into smaller, simpler tasks. Try to describe what you're doing, then make each step a method call. If you describe the algorithm in plain English, you might see how to break your program down into smaller chunks. I'll give you this to start you off:
June started on a Thursday.
I see you do have to use a switch: this is obviously partially an exercise about switch.
I suggest you go through your loop with a fine‑toothed comb and write down the values of the indices and how many spaces you have moved to the right for each iteration. That might help. I also suggest you change your heading to this for a bit, and change it back to its original state later:-I don't think that is a better way to print the days of the week, but it may help you envision the number of spaces from the left margin.

Campbell Ritchie wrote:


so that those format specifiers aren't all smushed together. Yeah, it's a longer format string but I find the spaces make it a little easier on the eyes and brain.
The reason I used 4 was so OP can count and work out that Thursday, being four days on from Sunday, starts on space 4 × 4 = 16. I thought he would revert to the original format for that String once the 16 is no longer required. I did say I didn't think it was the best way to print day names.

This thread has been viewed 1721 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 10, 2018 10:06:49.