Alisa Alisa - 11 months ago 38
Java Question

How to count number of times a Card Value appears in a Deck of Cards

I'm trying to find a way to count the number of times a card value appears in a deck of cards. I've created classes for Card and DeckHand. In the DeckHand class, I'm trying to create a method that allows the user to pick a card value to search and count. I've tried doing this a couple different ways and nothing seems to be working. Any ideas?

Card Class:

class Card {
private int _value, _suit;
private String[] _cardValues = {null, "Ace", "2", "3", "4","5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
private String[] _cardSuits = {null, "Clubs", "Diamonds", "Hearts", "Spades"};

public Card(int value,int suit) {
_value = value;
_suit = suit;
public int getCardValue() {
return _value;
public int getCardSuit() {
return _suit;
public String toString() {
return _cardValues[_value] + " of " + _cardSuits[_suit];

Deck class:

class DeckHand{
private Card[] _deckHand;
private int _deckSize;
private static final int MAXSIZE = 52;
private Card[] newDeck;

public DeckHand() {
_deckHand = new Card[MAXSIZE];
_deckSize = 0;
public DeckHand(int deckSize) {
_deckHand = new Card[MAXSIZE];
int index = 0;
for (int suit = 1; suit <= 4; suit++) {
for (int rank = 1; rank <= 13; rank++) {
_deckHand[index] = new Card(rank, suit);
_deckSize = deckSize;
public int count(int value){
int count = 0;
for (int i = 0; i<=_deckSize; i++) {
return count;


public class Program4 {
static Scanner keyboard = new Scanner(;
public static void main(String[] args) {
int suit, value;
DeckHand standard = new DeckHand(52);
System.out.println("\nWhich card value would you like to count?");
String[] values = {null, "Ace", "2", "3", "4","5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
System.out.println("Card Value Options: "
+ "\n-------------------");
for(int i = 1;i<values.length; i++) {
System.out.println(i + " - " + values[i]);
System.out.print("\tOption: ");
value = keyboard.nextInt();
+ "-----------------------------\n"
+ "The card value " + value appears " + standard.count(value)
+ " times in the deck."

I'm trying to get the program to ask the user to pick a value to look for in the array of cards and count how many times that card value appears (i.e. 2's or 5's) and then have that number outputted to the user (i.e. "That card value appears this # of times in the deck"). However, when I try to run this, all I get is "That card value appears 0 times in the deck" or I get an error saying there's something wrong with this line :


This is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
at program4.DeckHand.count(
at program4.Program4.main(
snippets\run.xml:53: Java returned: 1

Answer Source

The exception is caused by your for loop: It runs for 0 up to 52 inclusive. Your cards array has a max size of 52 so that means it has cards from 0-51(52 values). When you try to access the card at index 52 you get an indexArrayOutOfBoumdsException, because that simply does not exist. Indtead you should change your for loop from:

for(int i = 0;i<=_deckSize;i++){


for(int i = 0;i<_deckSize;i++){

Notice how the "=" is gone, now it will never access index 52 or the 53rd card of the deck.