3 Replies - 496 Views - Last Post: 07 May 2012 - 10:52 AM Rate Topic: -----

#1 naiiimah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 07-May 12

Deck of Cards, erray: IndexOutOfBoundsException: Index: 0, Size: 0

Posted 07 May 2012 - 08:55 AM

So I'm getting an out of bound error. I've tried to change the for-loop in deal() to for(int i=1, i<numberCards +1, i++). I just dont understand the error.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at PJ4.Deck.deal(PlayingCard.java:218)
at PJ4.Deck.main(PlayingCard.java:283)


package PJ4;

import java.util.*;
import java.util.ArrayList;

/** class PlayingCardException: It is used for errors related to Card and Deck objects
 *  Do not modify this class!
 */
class PlayingCardException extends Exception {

    /* Constructor to create a PlayingCardException object */
    PlayingCardException() {
        super();
    }

    PlayingCardException(String reason) {
        super(reason);
    }
}

/** class Card : for creating playing card objects
 *  it is an immutable class.
 *  Rank - valid values are 1 to 13
 *  Suit - valid values are 0 to 3
 *  Do not modify this class!
 */
class Card {

    /* constant suits and ranks */
    static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades"};
    static final String[] Rank = {"", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    /* Data field of a card: rank and suit */
    private int cardRank;  /* values: 1-13 (see Rank[] above) */

    private int cardSuit;  /* values: 0-3  (see Suit[] above) */

    /* Constructor to create a card */
    /* throw PlayingCardException if rank or suit is invalid */

    public Card(int rank, int suit) throws PlayingCardException {
        if ((rank < 1) || (rank > 13)) {
            throw new PlayingCardException("Invalid rank:" + rank);
        } else {
            cardRank = rank;
        }
        if ((suit < 0) || (suit > 3)) {
            throw new PlayingCardException("Invalid suit:" + suit);
        } else {
            cardSuit = suit;
        }
    }

    /* Accessor and toString */
    /* You may impelemnt equals(), but it will not be used */
    public int getRank() {
        return cardRank;
    }

    public int getSuit() {
        return cardSuit;
    }

    @Override
    public String toString() {
        return Rank[cardRank] + " " + Suit[cardSuit];
    }

    /* Few quick tests here */
    public static void main(String args[]) {
        try {
            Card c1 = new Card(1, 3);
            System.out.println(c1);
            c1 = new Card(10, 0);
            System.out.println(c1);
            c1 = new Card(10, 5);  // generate exception here
        } catch (PlayingCardException e) {
            System.out.println("PlayingCardException: " + e.getMessage());
        }
    }
}

/** class Deck represents a deck of 52 playing cards
 *  Use class Card to construct 52 playing cards!
 *  Do not add new data fields or modify defined methods
 *  You may add private methods 
 */
class Deck {

    /* this is used to keep track of original 52 cards */
    /** @originalDeck keeps track of original 52 cards
     */
    private List<Card> originalDeck;

    /* this starts with 52 cards deck from original deck   */
    /* it is used to keep track of remaining cards to deal */
    /* see reset(): it resets dealDeck to a original deck  */
    private List<Card> dealDeck;
    private List<Card> dealtDeck;
    
      

    /**
     * Constructor: Creates 52 playing cards in originalDeck and
     * 		    copy them to dealDeck.
     *
     * Note: You need to catch PlayingCardException from Card constructor
     *	     Use ArrayList for both originalDeck & dealDeck
     */
    public Deck() {
        // implement this method!
        originalDeck = new ArrayList<Card>(52);
        Card newDeck = null;



        for (int i = 0; i < 4; i++) {
            for (int j = 1; j < 14; j++) {
                try {
                    newDeck = new Card(j, i);

                } catch (PlayingCardException e) {
                    System.out.println(e.getMessage());
                }
                originalDeck.add(newDeck);
            }

        }

        dealDeck = new ArrayList<Card>();
        for (int i = 0; i <52; i++) {
            dealDeck.add(originalDeck.get(i));

        }
        


    }

    /**
     * Task: Shuffles cards in deal deck.
     * Hint: Look at java.util.Collections
     */
    public void shuffle() {

        assert (dealDeck != null);
        Collections.shuffle(dealDeck);
    }

    /**
     * Task: Deals cards from the deal deck.
     *
     * @param numberCards number of cards to deal
     * @return a list containing cards that were dealt
     * @throw PlayingCardException if numberCard > number of remaining cards
     *
     * Note: You need to create ArrayList to stored dealt cards
     *       and should removed dealt cards from dealDeck
     *
     */
    public List<Card> deal(int numberCards) throws PlayingCardException 
    {

        dealtDeck = new ArrayList<Card>(numberCards);


        for (int i = 0; i < numberCards; i++) {
            dealtDeck.add(dealDeck.remove(0));
        }
    
        if (numberCards > dealDeck.size()) {
            throw new PlayingCardException("Not enough cards to deal");
        }

  
    }

    /**
     * Task: Resets deal deck by getting all cards from the original deck.
     */
    public void reset() {
        dealDeck.clear();
        for (int i = 0; i <52; i++) {
            dealDeck.add(originalDeck.get(i));

        }
        
    }

    /**
     * Task: Return number of remaining cards in deal deck.
     */
    public int remain() {
        return dealDeck.size();
    }

    /**
     * Task: Returns a string representing cards in the deal deck 
     */
    @Override
    public String toString() {
        return "" + dealDeck;
    }

    /* Quick test                */
    /* Do not modify these tests */
    public static void main(String args[]) {
        int numHands = 3;
        int cardsPerHand = 15;
        Deck deck = new Deck();

        for (int j = 0; j < 2; j++) {
            System.out.println("===========================");
            System.out.println("Before shuffle:" + deck);
            System.out.println("===========================");

            for (int i = 0; i < numHands; i++) {
                deck.shuffle();
                System.out.println("Shuffle:" + deck.remain() + " cards:" + deck);
                try {
                    System.out.println("\n\nHand " + i + ":" + cardsPerHand + " cards:" + deck.deal(cardsPerHand));
                } catch (PlayingCardException e) {
                    System.out.println(e.getMessage());
                }
                System.out.println("\n\nRemain:" + deck.remain() + " cards:" + deck);
                System.out.println("===========================");
            }
            deck.reset();
        }

        try {
            deck.deal(55);
        } catch (PlayingCardException e) {
            System.out.println("PlayingCardException: " + e.getMessage());
        }
        System.out.println("===========================");
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Deck of Cards, erray: IndexOutOfBoundsException: Index: 0, Size: 0

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,689
  • Joined: 19-March 11

Re: Deck of Cards, erray: IndexOutOfBoundsException: Index: 0, Size: 0

Posted 07 May 2012 - 08:59 AM

Quote

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at PJ4.Deck.deal(PlayingCard.java:218)
at PJ4.Deck.main(PlayingCard.java:283)



This means that your Deck's main() method called the deal() method, which tried to remove() an item from an ArrayList which was of size zero. The call to the remove occurred on line 218, but the problem is that you haven't added anything to that ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 naiiimah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 07-May 12

Re: Deck of Cards, erray: IndexOutOfBoundsException: Index: 0, Size: 0

Posted 07 May 2012 - 10:14 AM

===========================
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Before shuffle:[A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades]
===========================
Shuffle:52 cards:[3 Diamonds, 7 Diamonds, A Clubs, 3 Clubs, K Hearts, 7 Spades, 4 Spades, 2 Diamonds, 8 Clubs, Q Clubs, J Hearts, 9 Spades, 2 Spades, A Diamonds, 3 Hearts, 4 Hearts, J Spades, 4 Diamonds, 10 Diamonds, 10 Clubs, 2 Clubs, A Spades, 6 Spades, J Clubs, 9 Diamonds, 9 Hearts, 7 Hearts, 7 Clubs, 10 Spades, 8 Diamonds, 5 Hearts, 5 Clubs, 2 Hearts, 5 Spades, 6 Hearts, K Diamonds, A Hearts, J Diamonds, 5 Diamonds, 6 Clubs, 8 Hearts, K Spades, Q Spades, Q Hearts, 4 Clubs, K Clubs, 6 Diamonds, 9 Clubs, 3 Spades, 10 Hearts, 8 Spades, Q Diamonds]
orig: 52 deal: 37 dealt: [3 Diamonds, 7 Diamonds, A Clubs, 3 Clubs, K Hearts, 7 Spades, 4 Spades, 2 Diamonds, 8 Clubs, Q Clubs, J Hearts, 9 Spades, 2 Spades, A Diamonds, 3 Hearts]
orig: 52 deal: 37 dealt: [3 Diamonds, 7 Diamonds, A Clubs, 3 Clubs, K Hearts, 7 Spades, 4 Spades, 2 Diamonds, 8 Clubs, Q Clubs, J Hearts, 9 Spades, 2 Spades, A Diamonds, 3 Hearts]


Hand 0:15 cards:[3 Diamonds, 7 Diamonds, A Clubs, 3 Clubs, K Hearts, 7 Spades, 4 Spades, 2 Diamonds, 8 Clubs, Q Clubs, J Hearts, 9 Spades, 2 Spades, A Diamonds, 3 Hearts]


Remain:37 cards:[4 Hearts, J Spades, 4 Diamonds, 10 Diamonds, 10 Clubs, 2 Clubs, A Spades, 6 Spades, J Clubs, 9 Diamonds, 9 Hearts, 7 Hearts, 7 Clubs, 10 Spades, 8 Diamonds, 5 Hearts, 5 Clubs, 2 Hearts, 5 Spades, 6 Hearts, K Diamonds, A Hearts, J Diamonds, 5 Diamonds, 6 Clubs, 8 Hearts, K Spades, Q Spades, Q Hearts, 4 Clubs, K Clubs, 6 Diamonds, 9 Clubs, 3 Spades, 10 Hearts, 8 Spades, Q Diamonds]
===========================
Shuffle:37 cards:[J Diamonds, 5 Diamonds, 6 Spades, 9 Diamonds, 10 Hearts, 8 Diamonds, K Spades, 6 Diamonds, J Spades, 8 Hearts, A Spades, 10 Spades, 5 Hearts, 5 Spades, K Diamonds, 9 Clubs, 6 Clubs, 2 Clubs, A Hearts, 7 Hearts, 9 Hearts, Q Spades, 8 Spades, Q Diamonds, 5 Clubs, 4 Hearts, Q Hearts, 3 Spades, 10 Clubs, J Clubs, K Clubs, 4 Clubs, 2 Hearts, 10 Diamonds, 4 Diamonds, 6 Hearts, 7 Clubs]
orig: 52 deal: 22 dealt: [J Diamonds, 5 Diamonds, 6 Spades, 9 Diamonds, 10 Hearts, 8 Diamonds, K Spades, 6 Diamonds, J Spades, 8 Hearts, A Spades, 10 Spades, 5 Hearts, 5 Spades, K Diamonds]
orig: 52 deal: 22 dealt: [J Diamonds, 5 Diamonds, 6 Spades, 9 Diamonds, 10 Hearts, 8 Diamonds, K Spades, 6 Diamonds, J Spades, 8 Hearts, A Spades, 10 Spades, 5 Hearts, 5 Spades, K Diamonds]


Hand 1:15 cards:[J Diamonds, 5 Diamonds, 6 Spades, 9 Diamonds, 10 Hearts, 8 Diamonds, K Spades, 6 Diamonds, J Spades, 8 Hearts, A Spades, 10 Spades, 5 Hearts, 5 Spades, K Diamonds]


Remain:22 cards:[9 Clubs, 6 Clubs, 2 Clubs, A Hearts, 7 Hearts, 9 Hearts, Q Spades, 8 Spades, Q Diamonds, 5 Clubs, 4 Hearts, Q Hearts, 3 Spades, 10 Clubs, J Clubs, K Clubs, 4 Clubs, 2 Hearts, 10 Diamonds, 4 Diamonds, 6 Hearts, 7 Clubs]
===========================
Shuffle:22 cards:[9 Hearts, 6 Hearts, 7 Hearts, 4 Clubs, J Clubs, 6 Clubs, A Hearts, 2 Clubs, K Clubs, 5 Clubs, 2 Hearts, 4 Diamonds, 9 Clubs, 10 Diamonds, Q Diamonds, Q Hearts, 3 Spades, 4 Hearts, 10 Clubs, 8 Spades, 7 Clubs, Q Spades]
orig: 52 deal: 7 dealt: [9 Hearts, 6 Hearts, 7 Hearts, 4 Clubs, J Clubs, 6 Clubs, A Hearts, 2 Clubs, K Clubs, 5 Clubs, 2 Hearts, 4 Diamonds, 9 Clubs, 10 Diamonds, Q Diamonds]
orig: 52 deal: 7 dealt: [9 Hearts, 6 Hearts, 7 Hearts, 4 Clubs, J Clubs, 6 Clubs, A Hearts, 2 Clubs, K Clubs, 5 Clubs, 2 Hearts, 4 Diamonds, 9 Clubs, 10 Diamonds, Q Diamonds]


Hand 2:15 cards:[9 Hearts, 6 Hearts, 7 Hearts, 4 Clubs, J Clubs, 6 Clubs, A Hearts, 2 Clubs, K Clubs, 5 Clubs, 2 Hearts, 4 Diamonds, 9 Clubs, 10 Diamonds, Q Diamonds]


Remain:7 cards:[Q Hearts, 3 Spades, 4 Hearts, 10 Clubs, 8 Spades, 7 Clubs, Q Spades]
===========================
===========================
Before shuffle:[A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades]
===========================
Shuffle:52 cards:[A Diamonds, 7 Diamonds, 2 Spades, 7 Clubs, 3 Clubs, A Hearts, 9 Diamonds, K Diamonds, 8 Hearts, 5 Diamonds, 6 Spades, 10 Hearts, K Clubs, K Spades, 3 Spades, 9 Spades, Q Spades, 4 Spades, 9 Clubs, 6 Hearts, 3 Diamonds, 4 Clubs, 5 Hearts, 10 Spades, Q Diamonds, 5 Clubs, Q Hearts, K Hearts, J Clubs, 9 Hearts, A Spades, 8 Spades, 7 Spades, 2 Clubs, 10 Diamonds, 10 Clubs, 6 Diamonds, 8 Diamonds, J Spades, 3 Hearts, J Diamonds, 8 Clubs, 2 Diamonds, J Hearts, 4 Hearts, Q Clubs, 6 Clubs, A Clubs, 2 Hearts, 5 Spades, 7 Hearts, 4 Diamonds]
orig: 52 deal: 37 dealt: [A Diamonds, 7 Diamonds, 2 Spades, 7 Clubs, 3 Clubs, A Hearts, 9 Diamonds, K Diamonds, 8 Hearts, 5 Diamonds, 6 Spades, 10 Hearts, K Clubs, K Spades, 3 Spades]
orig: 52 deal: 37 dealt: [A Diamonds, 7 Diamonds, 2 Spades, 7 Clubs, 3 Clubs, A Hearts, 9 Diamonds, K Diamonds, 8 Hearts, 5 Diamonds, 6 Spades, 10 Hearts, K Clubs, K Spades, 3 Spades]


Hand 0:15 cards:[A Diamonds, 7 Diamonds, 2 Spades, 7 Clubs, 3 Clubs, A Hearts, 9 Diamonds, K Diamonds, 8 Hearts, 5 Diamonds, 6 Spades, 10 Hearts, K Clubs, K Spades, 3 Spades]


Remain:37 cards:[9 Spades, Q Spades, 4 Spades, 9 Clubs, 6 Hearts, 3 Diamonds, 4 Clubs, 5 Hearts, 10 Spades, Q Diamonds, 5 Clubs, Q Hearts, K Hearts, J Clubs, 9 Hearts, A Spades, 8 Spades, 7 Spades, 2 Clubs, 10 Diamonds, 10 Clubs, 6 Diamonds, 8 Diamonds, J Spades, 3 Hearts, J Diamonds, 8 Clubs, 2 Diamonds, J Hearts, 4 Hearts, Q Clubs, 6 Clubs, A Clubs, 2 Hearts, 5 Spades, 7 Hearts, 4 Diamonds]
===========================
Shuffle:37 cards:[J Spades, 4 Spades, J Hearts, 3 Diamonds, 2 Clubs, 4 Hearts, 4 Diamonds, A Spades, J Clubs, 8 Diamonds, 4 Clubs, 8 Clubs, Q Clubs, 10 Clubs, 10 Diamonds, K Hearts, 3 Hearts, 8 Spades, 6 Diamonds, 9 Hearts, 7 Spades, 5 Hearts, 2 Diamonds, 6 Hearts, J Diamonds, 6 Clubs, Q Diamonds, A Clubs, 10 Spades, Q Spades, 5 Clubs, 7 Hearts, 9 Clubs, 2 Hearts, Q Hearts, 9 Spades, 5 Spades]
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
orig: 52 deal: 22 dealt: [J Spades, 4 Spades, J Hearts, 3 Diamonds, 2 Clubs, 4 Hearts, 4 Diamonds, A Spades, J Clubs, 8 Diamonds, 4 Clubs, 8 Clubs, Q Clubs, 10 Clubs, 10 Diamonds]
at java.util.ArrayList.remove(ArrayList.java:387)
orig: 52 deal: 22 dealt: [J Spades, 4 Spades, J Hearts, 3 Diamonds, 2 Clubs, 4 Hearts, 4 Diamonds, A Spades, J Clubs, 8 Diamonds, 4 Clubs, 8 Clubs, Q Clubs, 10 Clubs, 10 Diamonds]

at PJ4.Deck.deal(PlayingCard.java:168)

at PJ4.Deck.main(PlayingCard.java:233)
Hand 1:15 cards:[J Spades, 4 Spades, J Hearts, 3 Diamonds, 2 Clubs, 4 Hearts, 4 Diamonds, A Spades, J Clubs, 8 Diamonds, 4 Clubs, 8 Clubs, Q Clubs, 10 Clubs, 10 Diamonds]


Remain:22 cards:[K Hearts, 3 Hearts, 8 Spades, 6 Diamonds, 9 Hearts, 7 Spades, 5 Hearts, 2 Diamonds, 6 Hearts, J Diamonds, 6 Clubs, Q Diamonds, A Clubs, 10 Spades, Q Spades, 5 Clubs, 7 Hearts, 9 Clubs, 2 Hearts, Q Hearts, 9 Spades, 5 Spades]
===========================
Shuffle:22 cards:[6 Diamonds, 2 Diamonds, Q Hearts, 3 Hearts, A Clubs, 9 Spades, 5 Spades, 7 Hearts, 10 Spades, J Diamonds, 9 Clubs, K Hearts, Q Diamonds, 6 Hearts, 7 Spades, 5 Hearts, 5 Clubs, 8 Spades, Q Spades, 9 Hearts, 6 Clubs, 2 Hearts]
orig: 52 deal: 7 dealt: [6 Diamonds, 2 Diamonds, Q Hearts, 3 Hearts, A Clubs, 9 Spades, 5 Spades, 7 Hearts, 10 Spades, J Diamonds, 9 Clubs, K Hearts, Q Diamonds, 6 Hearts, 7 Spades]
orig: 52 deal: 7 dealt: [6 Diamonds, 2 Diamonds, Q Hearts, 3 Hearts, A Clubs, 9 Spades, 5 Spades, 7 Hearts, 10 Spades, J Diamonds, 9 Clubs, K Hearts, Q Diamonds, 6 Hearts, 7 Spades]


Hand 2:15 cards:[6 Diamonds, 2 Diamonds, Q Hearts, 3 Hearts, A Clubs, 9 Spades, 5 Spades, 7 Hearts, 10 Spades, J Diamonds, 9 Clubs, K Hearts, Q Diamonds, 6 Hearts, 7 Spades]


Remain:7 cards:[5 Hearts, 5 Clubs, 8 Spades, Q Spades, 9 Hearts, 6 Clubs, 2 Hearts]
===========================
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


here is the copy of my run. So the index out bound error runs before the test even begins. is the error because of how I coded to reset()? When I debug it I see that in the second loop, when there are only 7 cards remaining the cards are dealt again, shouldn't the exception being thrown stop that?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,689
  • Joined: 19-March 11

Re: Deck of Cards, erray: IndexOutOfBoundsException: Index: 0, Size: 0

Posted 07 May 2012 - 10:52 AM

Quote

at PJ4.Deck.deal(PlayingCard.java:168)
at PJ4.Deck.main(PlayingCard.java:233)


So evidently, you haven't got anything in this ArrayList when you try to remove something from it. Take a look at the main method, line 233, and figure out how it is that you get to there without anything in the Deck. Fix that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1