0

I have a problem in my Java program Black Jack. I can't seem to work out my "this.cards[o++]" as it always goes into ArrayIndexOutOfBoundException. But if I change it into "this.cards[j] OR this.cards[i]" it doesn't get an error but I know its wrong.

Here's the errors for:

**this.cards[o++]** 
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
        at CardDeck.<init>(CardDeck.java:18)
        at BlackJoker.main(BlackJoker.java:17)

**this.cards[j]**
null
Kc
Qc
8c
null
null

**this.cards[i]**
null
Ac
Ah
null
null
Ad

Here's my code:

import java.util.*;
public class CardDeck
{
    private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "J", "Q", "K", "A" };
    private char[] suits = {'s','h','d','c'};
    private Card[] cards = new Card[13];
    private int currentCard;

    CardDeck()
    {
        Card newCard;
        int o = 0;
        for(int i=0; i<this.suits.length; i++)
        {   
            for(int j=0; j<this.ranks.length; j++)
            {
                this.cards[o++] = new Card(this.ranks[j], this.suits[i]);
            }
        }
        this.shuffle();
    }

    public void testing() //just for testing 
    {
        System.out.println(this.suits[0]);
    }   

    public Card drawNextCard()
    {

        return cards[currentCard++];
    }

    private void shuffle()
    {
        Card[] tmp = new Card[13];
        for (int i = 0; i < cards.length; i++)
        {
            int index = (int)(Math.random() * (cards.length));

            tmp[index] = cards[i];
            cards[i] = cards[index];
            cards[index] = tmp[i];
        } 
    }
}

public class BlackJoker
{
    public static void main(String[] args)
    {
        CardDeck cardDeck = new CardDeck();

        //cardDeck.testing();
        System.out.println(cardDeck.drawNextCard());
        System.out.println(cardDeck.drawNextCard());
        System.out.println(cardDeck.drawNextCard());
        System.out.println(cardDeck.drawNextCard());
        System.out.println(cardDeck.drawNextCard());
        System.out.println(cardDeck.drawNextCard());
        //System.out.println(cardDeck.drawNextCard());
    }
}

2
  • Notice that "this.cards[j]" gives me a constant 'c' suits then "this.cards[i]" gives me a constant 'A' (ace). Can someone please explain to me what I've done wrong? Thank you so much for any help and advice Commented Oct 6, 2016 at 19:53
  • 1
    Best to have abstractions Card and Deck, where Deck has a collection of Cards. You can use the Collections.shuffle() method that's built in. tutorialspoint.com/java/util/collections_shuffle.htm Commented Oct 6, 2016 at 20:02

2 Answers 2

1

You are allocating an array that will only hold 13 cards:

private Card[] cards = new Card[13];

Try making it 52:

private Card[] cards = new Card[52];
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to increasing the card array length , In the shuffle function , you are calculating the index using Math.random, that causes ArrayIndexOutbound exception if the index is beyond the card array length. You need to handle this.

1 Comment

Not a problem. Since Math.random() always returns less than 1, (int)(Math.random() * (cards.length)) will be less than cards.length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.