0

Before I get into anything, I just want to say that I am a newbie to Java (1 year) and Stack Overflow (about a few months), so if I am doing something wrong, please let me know!

Anyways, lets get into the problem. You see, I am building a Poker program, and I am at the point where I need to "deal" out cards to all of the players (the user and 4 bots). To make things as fast and efficient as possible, I decided to use a for loop to add all of the cards. I have also done the same thing for resetting the deck every hand, using the following code:

public String[] cardTypes = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"} ;
...
// The for loop below resets the deck by adding all of the cards by using the array "cardTypes" (Ace, 2, Queen, etc.)
      
      for (int i = 0 ; i < poker.cardTypes.length ; i++) {
        poker.deck.add(poker.cardTypes[i] + " of Spades") ;
        poker.deck.add(poker.cardTypes[i] + " of Hearts") ;
        poker.deck.add(poker.cardTypes[i] + " of Clubs") ;
        poker.deck.add(poker.cardTypes[i] + " of Diamonds") ;
      }

Anyways, seems simple, right? Well, when I try to do that for all of the players, it crashes and gives me this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

This is my code:

static void dealOutHands() {
    Poker poker = new Poker() ;
    int pickedCard ;

    for (int i = 0 ; i < 2 ; i++) {
      pickedCard = (int) (Math.random() * poker.deck.size()) ;
      poker.playerHand.add(poker.deck.get(pickedCard)) ;
      poker.deck.remove(pickedCard) ;
    }

    for (int i = 0 ; i < 2 ; i++) {
      pickedCard = (int) (Math.random() * poker.deck.size()) ;
      poker.botHand1.add(poker.deck.get(pickedCard)) ;
      poker.deck.remove(pickedCard) ;
    }

    for (int i = 0 ; i < 2 ; i++) {
      pickedCard = (int) (Math.random() * poker.deck.size()) ;
      poker.botHand2.add(poker.deck.get(pickedCard)) ;
      poker.deck.remove(pickedCard) ;
    }

    for (int i = 0 ; i < 2 ; i++) {
      pickedCard = (int) (Math.random() * poker.deck.size()) ;
      poker.botHand3.add(poker.deck.get(pickedCard)) ;
      poker.deck.remove(pickedCard) ;
    }

    for (int i = 0 ; i < 2 ; i++) {
      pickedCard = (int) (Math.random() * poker.deck.size()) ;
      poker.botHand4.add(poker.deck.get(pickedCard)) ;
      poker.deck.remove(pickedCard) ;
    }
  }

For some reason, it keeps telling me that the error occurs here:

poker.playerHand.add(poker.deck.get(pickedCard)) ;

Can someone help? Thank you!

TimMcYeah
  • 121
  • 6
  • 2
    Because when the List is empty even starting at 0 (aka the first element) makes no sense as there is no first element. – OH GOD SPIDERS May 23 '23 at 15:17
  • 1
    If the comment above seems a bit vague it's because we don't have enough context to tell you which list is empty or why. Please [edit] your question to include a [example]. – Federico klez Culloca May 23 '23 at 15:20
  • You should make sure that `poker.deck` is correctly initialized and filled with elements. According ti the error message it is just an empty List so whatever you coded to initialize that List doesn't seem to work. – OH GOD SPIDERS May 23 '23 at 15:20
  • The other comments, plus a hint: `pickedCard = (int) (Math.random() * poker.deck.size()) ;` - if `poker.deck.size() == 0` (i.e. the deck is empty), `pickedCard` resolves to `0`. But since `deck` is empty, `deck.get(0)` will throw an exception – BackSlash May 23 '23 at 15:21
  • You didn't share the actual method with the issue nor the instance variables. – aled May 23 '23 at 15:21

0 Answers0