• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
            (Integer.valueOf(args[1]),
            Integer.valueOf(args[0])-1,1);

    printHeader(calendar);
    printData(calendar);           
  }

  public static void printHeader(Calendar calendar){

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

    String monthName = "";

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

    System.out.println(" "+monthName+ ", " +calendar.get(Calendar.YEAR));
    System.out.println("--------------------");
    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++){
      System.out.print("\n");
      calendar.add(Calendar.DATE,1);
    }
    System.out.printf("%4d",calendar.get(Calendar.DATE));
  }
}
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
David Alcorn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Alcorn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a class assignment and that is what's required.
Untitled.png
[Thumbnail for Untitled.png]
Assignment
1Untitled.png
[Thumbnail for 1Untitled.png]
I/O
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you remember to specify program arguments when you ran your program? Think about what happens when you don't.
 
David Alcorn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I set up the argument(correctly, I think but I am getting the following output...
Capture.PNG
[Thumbnail for Capture.PNG]
 
Junilu Lacar
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

Or

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.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!