Results 1 to 6 of 6
  1. #1
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default 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.
    ~pondwire
    Last edited by Eranga; 07-21-2011 at 08:08 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Check your while conditions: you seem to be confusing rank and suit.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I don't know exactly which array the compiler is talking about
    Time for some debugging code:

    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.

  4. #4
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    Thanks... I fixed that part and so that little tidbit is taken care of...

  5. #5
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    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...

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Changing size of Array
    By ravian in forum New To Java
    Replies: 3
    Last Post: 06-05-2012, 08:17 PM
  2. Doubling size of 2d array
    By osenna66 in forum New To Java
    Replies: 0
    Last Post: 07-03-2011, 04:30 AM
  3. size of array
    By swathi dharmaraj in forum New To Java
    Replies: 8
    Last Post: 04-22-2011, 12:35 AM
  4. Doubling the size of an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  5. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 10:37 PM

Posting Permissions

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