Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with 2d array

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with 2d array

    Hi,

    I am having trying to model a deck(s) of cards in Java and am having trouble with an array. I want to get a 2d array with the first set being the value of the card and the second being the suit.
    If i leave it as a 1d array, just adding the card values, everthying works fine but when I try to change to 2d all I get is Null.pointerException and ArrayIndexOutOfBounds errors.

    public class decks
    {
        // instance variables - replace the example below with your own
        private int numberOfDecks;
        private int totalNoCards;
        private String cardArray[][];
        private String oneDeck[] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
        private String suits[] = {"Clubs","Spades","Hearts","Diamonds"};
        /**
         * Constructor for objects of class decks
         */
        public decks(int numberOfDecks)
        {
            // initialise instance variables
            this.numberOfDecks = numberOfDecks;
            this.totalNoCards = numberOfDecks * 52;
            cardArray = new String[0][totalNoCards];
     
        }
     
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
     
        public void addCards() 
     
        {
            int counterOne = 1;
            int counterTwo = 0;
            int counterThree = 0;
            int counterFour = 0;
     
            while(counterOne <= numberOfDecks) {
                cardArray[0][counterTwo] = oneDeck[counterThree];
                counterTwo++;
                counterThree++;
                if(counterThree > 12) {
                    counterFour++;
                    counterThree = 0;
                    if(counterFour > 3) {
                        counterFour = 0;
                        counterOne++;
                    }
                }           
     
            }
        }
    }

    I am sure there is something simple I am missing. Any help is much appreciated.

    Thankyou


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with 2d array

    In Java, a 2D array is basically an array of arrays.

    So, you need to initialize the array of arrays, then initialize the "internal" arrays.

    It's much easier to create a Card object that has the suit and the value of the card than to use a 2D array.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Problem with 2d array

    Inspect the line where you create your array...you've initialized an array of an array, with the first array of length zero (it should be of length > 0)
     cardArray = new String[0][totalNoCards];

Similar Threads

  1. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  2. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  3. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM
  4. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 02:18 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 12:32 PM