Results 1 to 2 of 2
  1. #1
    cmonson is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Help with ArrayIndexOutOfBoundsException

    I have been trying to make this work forever and it will only work for complete decks and not incomplete ones help. It works for some values but not other ones like 55,15 and other ones.
    https://www.dropbox.com/s/iluuhuudiu7tsv1/HW%209.zip
    Java Code:
     public void make()throws ArrayIndexOutOfBoundsException{
             int i=0 , j=0 , q=0 ;
             do{
                if (j>12){
                   j=0;}
                q=0;
                do{
                   Card card2 = new Card(j,q);
                   allCards[i] = card2;
                   System.out.println(allCards[i]);  //Line 42
                   if(i>size){
                      break;}
                   i++;
                   q++;
                }while(q<4);
                j++;
             }while(i<=size-1);
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
    	at Deck.make(Deck.java:42)
    	at testDeck.main(testDeck.java:18)
          }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: Help with ArrayIndexOutOfBoundsException

    The issue is with the if statement. If the size of the array is 10 then valid indicies are 0-9. When i is 9 it is not greater than size, increments i to 10 (assuming q < 4) goes around loop again and attempts to access array at index 10 which is invalid.

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2011, 08:28 PM
  2. ArrayIndexOutOfBoundsException
    By np2392 in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 10:28 PM
  3. ArrayIndexOutOfBoundsException: 10 ??
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 06:56 PM
  4. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  5. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •