1

I am having an issue with my code that calculates an integer for the hand total in a game of Black Jack. For some reason the for loop is iterating the first time through twice, meaning that it uses i = 0 twice through the loop before incrementing i. Is there a reason why it is doing this? I don't know what to change.

  private void setTotal(){
    boolean ace = false;
    for (int i = 0; i<hand.size(); i++){
        if((hand.get(i).getValue()) == 1){
            handTotal += 1;
            ace = true;
        }
        else if((hand.get(i).getValue()) > 10){
            handTotal += 10;
        }
        else {
            handTotal += (hand.get(i).getValue());
        }
    }
    if(ace == true && handTotal<12){
        handTotal += 10;
    }
}

Below is the code with a bunch of print statements I've been using to check it.

   private void setTotal(){
    boolean ace = false;
    for (int i = 0; i<hand.size(); i++){
        System.out.println("start " + i);
        if((hand.get(i).getValue()) == 1){
            handTotal += 1;
            ace = true;
            System.out.println("ace one");
        }
        else if((hand.get(i).getValue()) > 10){
            handTotal += 10;
            System.out.println("face card");
        }
        else {
            handTotal += (hand.get(i).getValue());
            System.out.println("num");
        }
        System.out.println("end " + i);
    }
    if(ace == true && handTotal<12){
        handTotal += 10;
        System.out.println("ace plus");
    }

}

In the main, I run this

public class BlackJack {

public static void main(String[] args){
   Deck deck = new Deck();
   Dealer dealer = new Dealer();
   deck.shuffle();
   for(int i =0; i<2; i++){
       dealer.dealCard(deck.draw());
   }
   System.out.println(dealer.getHand());
   System.out.println(dealer.getHand().size());
   System.out.println(dealer.getTotal());
}

}

And for example it printed start 0 num end 0 start 0 num end 0 start 1 num end 1 [7 of Clubs, 4 of Clubs] 2 18

14
  • Which value do you get in hand.size() at first iteration? Commented Nov 12, 2014 at 1:20
  • Are you sure it's iterating twice with i = 1 ? or you get handTotal increased twice? Commented Nov 12, 2014 at 1:21
  • 1
    No your loop is ok. Most probably issue is that you are not setting handTotal to 0 before entering the loop. Commented Nov 12, 2014 at 1:21
  • 1
    Please explain what makes you think that the code is not working, and show the calling code. At the moment, we're all just guessing as to what might be the problem, but have nothing concrete to work with. Commented Nov 12, 2014 at 1:24
  • 1
    SJuan76 / Jason had the right idea. The handTotal is holding the old value calculated from the hand with 1 card and then incremented by the new value from the hand with 2 cards Commented Nov 12, 2014 at 1:35

1 Answer 1

2

I would prefer a for-each loop to implement setTotal (and don't forget to reset the handTotal to zero) like

private void setTotal() {
    handTotal = 0;
    boolean ace = false;
    for (Card c : hand) {
        int value = c.getValue();
        if (value == 1) {
            ace = true;
        }
        handTotal += (value > 10) ? 10 : value;
    }
    if (ace && handTotal < 12) {
        handTotal += 10;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry this is my first time taking any Java so can you explain this line handTotal += (value > 10) ? 10 : value;
@Julia It's a ternary operator. If value > 10, use 10. Else use value.
I'm very confused. I don't see a continue statement in her code at all.
Thank you, initializing handTotal to 0 fixes the problem and I will maybe try a for-each loop although that alone seems to have fixed it.

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.