title: Java Blackjack card total counting
body: <p>Here is the entirety of my Hand class below:</p>
  <p>The question that I have is that when I run the program, it gives me output like:</p>
  <blockquote>
  <p>&quot;Welcome to blackjack...dealing 2 cards:</p>
  <p>Here are your cards: [AH, 3S]<br />
  10 , (h)it or (s)tand?
  &quot;</p>
  </blockquote>
  <p>An Ace of Hearts and a 3 of spades should not give me 10. I do not know what's wrong with my code. My <code>getSoftTotal()</code> method looks like it should work fine (I haven't bothered with my <code>getHardTotal()</code> method yet.</p>
  <p>Here's another output:</p>
  <blockquote>
  <p>&quot;Here are your cards: [4D, 10D]<br />
  25 , (h)it or (s)tand? &quot;</p>
  </blockquote>
  <pre><code>import java.util.ArrayList;
  import java.util.Scanner;
  
  public class Hand extends Deck {
  
      private static ArrayList&lt;Card&gt; cards;
  
      public Hand() {
  
          cards = new ArrayList&lt;Card&gt;();
      }
  
      public void addCard(Card other) {
  
          cards.add(other);
      }
  
      public static boolean hasBlackjack() {
  
          boolean ace = true;
          boolean ten = true;
  
          for (int i = 0; i &lt; cards.size(); i++) {
              if (!(cards.get(i).getValue() == Card.ACE))
                  ace = false;
              if (!(cards.get(i).getValue() == 10))
                  ten = false;
          }
          return (ace &amp;&amp; ten);
      }
  
      public static boolean isBusted() {
          return getTotal() &gt; 21;
  
      }
  
      public static int getSoftTotal() {
          int total = 0;
          for (Card card : cards) {
              total += card.getValue();
          }
          return total;
      }
  
      public static int getTotal() {
  
          return getSoftTotal(); // soft
      }
  
      @SuppressWarnings(&quot;static-access&quot;)
      public static void main(String[] args) {
  
          Scanner input = new Scanner(System.in);
  
          Deck deck; // A deck of cards. A new deck for each game.
          String hit = &quot;&quot;;
  
          Hand hand = new Hand();
          deck = new Deck();
          deck.shuffle();
  
          System.out.println(&quot;Welcome to blackjack...dealing 2 cards:&quot;);
          System.out.println(&quot;&quot;);
  
          hand.addCard(deck.deal());
          hand.addCard(deck.deal());
  
          do {
              System.out.println(&quot;Here are your cards: &quot; + cards.toString());
              hand.addCard(deck.deal());
              System.out.println(hand.getTotal() + &quot; , (h)it or (s)tand? &quot;);
              System.out.println(&quot;&quot;);
              hit = input.nextLine();
          } while (hit.equals(&quot;h&quot;) &amp;&amp; isBusted() == false
                  &amp;&amp; hasBlackjack() == false);
  
          if (isBusted() == true) {
              System.out.println(&quot;BUSTED!  YOU LOSE!&quot;);
          }
          if (hasBlackjack() == true) {
              System.out.println(&quot;BLACKJACK!  YOU WIN!&quot;);
          }
          if (hit.equals(&quot;s&quot;) &amp;&amp; isBusted() == false) {
              System.out.println(&quot;You ended with &quot; + getTotal());
          }
      }
  
  }
  </code></pre>
  <p><strong>Card.java</strong></p>
  <pre><code>public class Card {
  
  public final static int CLUBS = 0;
  public final static int HEARTS = 1;
  public final static int SPADES = 2;
  public final static int DIAMONDS = 3;
  
  // Numbers for the values
  public final static int ACE = 1;
  public final static int JACK = 11;
  public final static int QUEEN = 12;
  public final static int KING = 13;
  
  /**
   * This card's suit, one of the constants SPADES, HEARTS, DIAMONDS, CLUBS,
   * or JOKER. The suit cannot be changed after the card is constructed.
   */
  private final int suit;
  
  /**
   * The card's value. For a normal cards, this is one of the values 1 through
   * 13, with 1 representing ACE. For a JOKER, the value can be anything. The
   * value cannot be changed after the card is constructed.
   */
  private final int value;
  
  /**
   * Creates a card with a specified suit and value.
   * 
   * @param theValue
   *            the value of the new card. For a regular card (non-joker), the
   *            value must be in the range 1 through 13, with 1 representing
   *            an Ace. You can use the constants Card.ACE, Card.JACK,
   *            Card.QUEEN, and Card.KING. For a Joker, the value can be
   *            anything.
   * @param theSuit
   *            the suit of the new card. This must be one of the values
   *            Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or
   *            Card.JOKER.
   * @throws IllegalArgumentException
   *             if the parameter values are not in the Permissible ranges
   */
  public Card(int cardValue, int cardSuit) {
      if (cardSuit &gt; 3 || cardSuit &lt; 0)
          throw new IllegalArgumentException(&quot;Illegal card suit:&quot; + cardSuit);
      if (cardValue &gt; 13 || cardValue &lt; 1)
          throw new IllegalArgumentException(&quot;Illegal card value:&quot;
                  + cardValue);
      value = cardValue;
      suit = cardSuit;
  }
  
  public int getSuit() {
      return suit;
  }
  
  public int getValue() {
      return value;
  }
  
  public String getSuitString() {
      switch (suit) {
  
      case CLUBS:
          return &quot;C&quot;;
      case HEARTS:
          return &quot;H&quot;;
      case SPADES:
          return &quot;S&quot;;
      case DIAMONDS:
          return &quot;D&quot;;
      default:
          return &quot;&quot;;
      }
  }
  
  /**
   * Returns a String representation of the card's value.
   * 
   * @return for a regular card, one of the strings &quot;Ace&quot;, &quot;2&quot;, &quot;3&quot;, ...,
   *         &quot;10&quot;, &quot;Jack&quot;, &quot;Queen&quot;, or &quot;King&quot;. For a Joker, the string is
   *         always numerical.
   */
  public String getValueString() {
  
      switch (value) {
      case 1:
          return &quot;A&quot;;
      case 2:
          return &quot;2&quot;;
      case 3:
          return &quot;3&quot;;
      case 4:
          return &quot;4&quot;;
      case 5:
          return &quot;5&quot;;
      case 6:
          return &quot;6&quot;;
      case 7:
          return &quot;7&quot;;
      case 8:
          return &quot;8&quot;;
      case 9:
          return &quot;9&quot;;
      case 10:
          return &quot;10&quot;;
      case 11:
          return &quot;J&quot;;
      case 12:
          return &quot;Q&quot;;
      case 13:
          return &quot;K&quot;; // Default will return King if it's not any of those up
  
      default:
          return &quot;&quot;;
  
      }
  }
  
  public String toString() {
  
      return getValueString() + &quot;&quot; + getSuitString();
  }
  
  public boolean equals(Card other) {
  
      if (this.value == other.value &amp;&amp; this.suit == other.suit){
          return true;}
      else{
      
          return false;
      }
  }
  
  public static void main(String[] args) {
  
      Card a = new Card(10, 3);
      System.out.println(a.toString());
  
      // Card b = new Card(14, 3); //ThrowsIllegalArgumentException
      // System.out.println(b.toString());
  
      Card c = new Card(1, 2);
      System.out.println(c.toString());
      
      Card equals1Yes = new Card(2, 3);
      Card equals2Yes = new Card(2, 3);
      
      Card notEquals = new Card(3, 2);
      
      System.out.println(equals1Yes.equals(equals2Yes));
      System.out.println(notEquals.equals(equals1Yes));
      
      System.out.println(a.getSuit());
      System.out.println(a.getValue());
      
      System.out.println(a.getValueString());
      System.out.println(a.getSuitString());
      
  
      
      
  
  }
  
  } // end class Card
  </code></pre>
  <p><strong>Deck.java</strong></p>
  <pre><code>import java.util.*;
  
  public class Deck {
  
  private ArrayList&lt;Card&gt; cards;
  
  public Deck() {
      cards = new ArrayList&lt;Card&gt;();
  
      for (int a = 1; a &lt;= 13; a++) {
          for (int b = 0; b &lt;= 3; b++) {
              cards.add(new Card(a, b));
          }
      }
  
  }
  
  public Card deal() {
      if (cards.size() != 0) {
          return cards.remove(0);
      } else {
          return null;
      }
  
  }
  
  public String toString() {
      String return1 = &quot;&quot;;
      for (int i = 0; i &lt;= cards.size(); i++) {
          return1 = +cards.indexOf(i) + &quot; &quot;;
      }
      return return1;
  
  }
  
  public int size() {
      return cards.size();
  }
  
  public void shuffle() {
  
      Collections.shuffle(cards);
  }
  
  public boolean isEmpty() {
      if (cards.size() == 0) {
          return true;
      } else {
          return false;
      }
  
  }
  
  public void reset() {
  
  }
  
  public static void main(String[] args) {
  
      
  
  }
  }
  </code></pre>