title: Using If statement in while loop, blackjack game
body: <p>At the end stages for finishing my Black-Jack game for school.
  I seem to be running into a problem while trying to program the logic.</p>
  
  <p>Basically the dealer will only hit if their cards are &lt;= 16, for some reason the if statement for this isn't working and the dealer just hits all the time or stays all the time, regardless of the value of their cards... I know it's probably something simple but I can't seem to find it.</p>
  
  <p>Also the dealer isn't busting when his card values are over 21.</p>
  
  <p><strong>main</strong> (PROBLEMS ARE HERE)</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  
  public class game {
  public static String name;
  
  public static void main(String[]args){
  
      deck doc = new deck();
      dealer dealer = new dealer(doc);
  
      Scanner nameScanner = new Scanner(System.in);
  
      int money = 200;
  
      System.out.println("Welcome to BlackJack!" + "\n\n" + "Your balance: $" + money + "\n\n" + "What is your name? ");
      name = nameScanner.nextLine();
  
      dealer.start();
  
      dealer.dealDealer();
      dealer.dealPlayer();
      dealer.dealDealer();
      dealer.dealPlayer();
  
  
      boolean dealerStay = false;
      boolean playerStay = false;
      boolean allStay = false;
      boolean tie = false;
  
      while(true){
  
          System.out.println(dealer.toString());
          Scanner gameScanner = new Scanner(System.in);   
  
          System.out.println("\nWould you like to Hit(H) or Stay(S)? ");
  
          String hos = gameScanner.nextLine();
  
          //Players &amp; Dealer hit logic
          if(hos.equalsIgnoreCase("h")) {
              dealer.dealPlayer();
          }
          //Player bust
          if(dealer.getPlayerHandValue() &gt; 21){
              System.out.println("*****YOU LOSE\n*****You bust with " + dealer.getPlayerHandValue() + "\n*****Dealer wins by default");
              break;
          }
          //Stay logic
          if(hos.equalsIgnoreCase("s")){
              playerStay = true;
          }
          //Dealer hit logic PROBLEM HERE
          if(playerStay == true &amp;&amp; dealer.getDealerHandValue() &lt;= 16){
              dealer.dealDealer();
              System.out.println("\n*****Dealer hit\n");
          }
          //Dealer stay logic PROBLEM HERE
          if (playerStay == true &amp;&amp; dealer.getDealerHandValue() &gt; 16){
              System.out.println("\n*****Dealer stayed\n");
              dealerStay = true;
          }
          //Dealer bust PROBLEM HERE
          if(dealer.getDealerHandValue() &gt; 21){
              System.out.println("*****YOU WIN\n *****Dealer busts");
              break;
          }
  
  
  
  
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Constructors</strong> (if you need them)</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  public class dealer {
  
  deck deck = new deck();
  String dealerTopCardString = "";
  card playerHandCard;
  card dealerHandCard;
  ArrayList&lt;card&gt; dealerHand = new ArrayList&lt;card&gt;();
  ArrayList&lt;card&gt; playerHand = new ArrayList&lt;card&gt;();
  
  public dealer(deck cards) {
      deck = cards;
  
  }
  
  public void start() {
      dealerHand = new ArrayList&lt;card&gt;();
      playerHand = new ArrayList&lt;card&gt;();
  }
  
  public String getPlayerHand() {
  
      String playerHandString = "";
      for (int i = 0; i &lt; playerHand.size(); i++) {
          playerHandCard = playerHand.get(i);
          card card = new card(playerHandCard.getValue(),
                  playerHandCard.getSuit());
          playerHandString += "\n";
          playerHandString += card.getValue();
          playerHandString += "\t";
          playerHandString += card.getSuit();
      }
      return playerHandString;
  
  }
  
  public String getDealerHand() {
  
      String dealerHandString = "";
      for (int i = 0; i &lt; dealerHand.size(); i++) {
          dealerHandCard = dealerHand.get(i);
          card card = new card(dealerHandCard.getValue(),
                  dealerHandCard.getSuit());
          dealerHandString += "\n";
          dealerHandString += card.getValue();
          dealerHandString += "\t";
          dealerHandString += card.getSuit();
      }
      return dealerHandString;
  }
  
  public void dealPlayer() {
  
      playerHand.add(deck.deal());
  
  }
  
  public void dealDealer() {
  
      dealerHand.add(deck.deal());
  
  }
  
  public String getDealerTopCard() {
      String dealerTopCard = "";
      card cardTop = dealerHand.get(0);
      card card = new card(cardTop.getValue(), cardTop.getSuit());
      dealerTopCard += "\n";
      dealerTopCard += card.getValue();
      dealerTopCard += "\t";
      dealerTopCard += card.getSuit();
      return dealerTopCard;
  
  }
  
  @Override
  public String toString() {
  
      return  "\n" + game.name + "'s cards : " + getPlayerHand() + "\n\n " + game.name + "'s total: "
              + getPlayerHandValue() + "\n\n" + "Dealer is showing : "
              + getDealerHand();
  }
  
  public int getPlayerHandValue() {
      int playerHandValue = 0;        
  
      for (card a : playerHand) {
          String c = a.getValue();
          if (c.equals("J")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("Q")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("K")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("A")) {
              playerHandValue += 11;
          }
  
          else {
              playerHandValue += Integer.parseInt(a.getValue());
  
          }
      }
  
      int valueOfCards = playerHandValue;
      return valueOfCards;
  }
  
  public int getDealerHandValue() {
      int playerHandValue = 0;        
  
      for (card a : playerHand) {
          String c = a.getValue();
          if (c.equals("J")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("Q")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("K")) {
              playerHandValue += 10;
          }
  
          else if (c.equals("A")) {
              playerHandValue += 11;
          }
  
          else {
              playerHandValue += Integer.parseInt(a.getValue());
  
          }
      }
  
      int valueOfCards = playerHandValue;
      return valueOfCards;
  }
  
  public void resetDealerHand() {
      dealerHand.clear();
  }
  
  public void resetPlayerHand() {
      playerHand.clear();
  }
  
  }
  </code></pre>
  
  <p>Let me know if I can provide you with anything else, thanks in advance for the help!</p>