Results 1 to 6 of 6
Thread: Array Size Error
- 07-20-2011, 07:39 AM #1
Array Size Error
Hello All: I get an array size error but I don't know what to change in my code. Here is the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Game.createAllCards(Game.java:71)
at Game.<init>(Game.java:50)
at testGame.main(testGame.java:14)
Here is my Game class through line 79 with 50 and 71 pointed out:
************Java Code:import java.util.*; public class Game { /**Two variables are declared, one for the number of cards in * the particular game and another for the maximum number of * of cards defined by the game. Four arrays are declared, * one each for the players, the suit & rank of the cards, * and the card list itself. **/ private int numCards; final int maxCards = 20; private Player[] players; final private String[] newSuit = {"CLUBS", "DIAMONDS", "HEARTS", "SPADES"}; final private int[] newRank = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private Card[] cards; /**The constructor of Game with one parameter to initialize * the number of cards chosen to <= maximum allowed. The size * is specified as an even number between 10 and 20. The * default size is the maximum, 20. A full deck of cards is * then created using the Card array and the createAllCards * method. That new deck is then shuffled in the shuffle * method. **/ public Game(int cardCount) { if (cardCount > maxCards || cardCount < 10 ||cardCount % 2 ==1) { System.out.println("Your game can only have between "); System.out.println("10 and 20 cards. 20 cards have been "); System.out.println("selected for you. Good luck."); cardCount = maxCards; } else numCards = cardCount; cards = new Card[numCards]; [COLOR="red"]Card[] newDeck = createAllCards();[/COLOR] shuffle(newDeck); if (numCards < maxCards) createNewGameDeck(newDeck); } //createAllCards method called private Card[] createAllCards() { Card[] allCards = new Card[maxCards]; int i, j; i = 0; //while loop creates cards based on number of elements //in each of the suit and rank arrays while(i < newSuit.length) { j = 0; while(j < newRank.length) { [COLOR="red"]Card c1 = new Card(newRank[i], newSuit[j]);[/COLOR] allCards[i * newSuit.length + j] = c1; j++; } i++; } return allCards; }
Here is line 14 from the testGame file:
Game game1 = new Game(keyboard.nextInt());
***********
I don't understand why the arrays are not large enough for a reasonable input of 15 or 20 cards. I don't know exactly which array the compiler is talking about or how to fix this. I appreciate any suggestions. Thanks.
~pondwireLast edited by Eranga; 07-21-2011 at 08:08 AM. Reason: code tags added
- 07-20-2011, 07:42 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Check your while conditions: you seem to be confusing rank and suit.
- 07-20-2011, 07:45 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Time for some debugging code:I don't know exactly which array the compiler is talking about
Java Code:while(i < newSuit.length) { j = 0; while(j < newRank.length) { [color=green]System.out.println("About to create card with rank ndx " + i + " and suit ndx " + j);[/color] Card c1 = new Card(newRank[i], newSuit[j]); allCards[i * newSuit.length + j] = c1; j++; } i++; }
[Edit]
The message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4" also provides a hint. That 4 is what it is grumbling about. It is newSuit that can't take an index of 4 (since there are only 4 suits indexed from zero to three)Last edited by pbrockway2; 07-20-2011 at 07:48 AM.
- 07-21-2011, 07:59 AM #4
Thanks... I fixed that part and so that little tidbit is taken care of...
- 07-21-2011, 08:01 AM #5
Hello: Thank you for the explanation. Sorry to reply so late, but I've been working on code so long I have no time to do anything else... and my code STILL won't compile. Oh well. I did fix what you and the other stellar contributor mentioned, but now I have a whole slew of new problems that weren't there before, like "illegal start of expression" all over the place when they look fine to me. NOthing works. Anyway, thanks again...
- 07-21-2011, 08:07 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you post the complete error you comes with?
Similar Threads
-
Changing size of Array
By ravian in forum New To JavaReplies: 3Last Post: 06-05-2012, 08:17 PM -
Doubling size of 2d array
By osenna66 in forum New To JavaReplies: 0Last Post: 07-03-2011, 04:30 AM -
size of array
By swathi dharmaraj in forum New To JavaReplies: 8Last Post: 04-22-2011, 12:35 AM -
Doubling the size of an array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:42 PM -
Array size declaration
By JT4NK3D in forum New To JavaReplies: 3Last Post: 01-18-2008, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks