Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: while loop just stops looping

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation while loop just stops looping

    Hey guys,

    I am writing a program to play blackjack automatically. I am having trouble though with a while loop. For some reason, it just stops. It should be outputting, so it isn't even like it is stuck in an infinite loop. There are no errors either. My code and an example of the output are below. As far as I can tell, this happens when either it should hit or stay. Can someone help me out?

    An example of the output I am getting (Obviously not running the 10 hands that it should)

    How many hands would you like to run?
    10
    What would you like the base bet to be?
    1
    Current balance is: 1000
    Player bets 1. Balance is now 999
    0
    1
    Player has: Jack, 10 for a value of 20. Dealer shows 7
    (18,5)
    Player stays.
    Dealer has 7, 7, 3 for 17
    Player beat dealer.
    Current balance is: 1001
    Player bets 3. Balance is now 998
    0
    1
    Player has: 10, 2 for a value of 12. Dealer shows 5
    (0,3)
    Player stays.
    Dealer has 3, 5, 8, 3 for 19
    Dealer beat player.
    Current balance is: 998
    Player bets 2. Balance is now 996
    0
    1
    Player has: 3, 9 for a value of 12. Dealer shows Jack
    (0,8)
    Player hits.
    2
    Player has: 3, 9, 2 for a value of 14. Dealer shows Jack
    (2,8)
    Player hits.
    3

    The part that is not working (the while loop is the issue, not the for loop. just added the for loop in case it helps you see what it should do.)
    for (int i = 0; i < hands.size(); i++) {
    					if (i == 1) 
    						continue;
     
    					int handIter = 0;
     
    					while (true) {
    						handIter++;
    						System.out.println(handIter);
     
    						p("Player has: " + hands.get(i) + " for a value of " + hands.get(i).getValue() + ". Dealer shows " + hands.get(1).getCard(1).getName());
     
    						if (hands.get(i).getValue() <= 21) {
    							String action = actions.getAction(hands.get(i), hands.get(1));
     
    							if (action.equals("HIT")) {
    								p("Player hits.");
     
    								hands.get(i).addCard(deck.draw());
    							} else if (action.equals("STAY")) {
    								p("Player stays.");
     
    								break;
    							} else if (action.equals("DOUBLE") && handIter == 1) {
    								p("Player doubles.");
     
    								if (bal - origBet >= 0) {
    									bal -= origBet;
    									bet += origBet;
     
    									hands.get(i).addCard(deck.draw());
     
    									break;
    								} else {
    									hands.get(i).addCard(deck.draw());
    								}
    							} else if (action.equals("SPLIT") && handIter == 1) {
    								if (bal - origBet < 0) {
    									p("Player is out of money!");
    									break game;
    								}
     
    								p("Player splits.");
     
    								hands.add(new Hand());
     
    								hands.get(hands.size() - 1).addCard(hands.get(i).removeCard(1));
    								hands.get(hands.size() - 1).addCard(deck.draw());
     
    								hands.get(i).addCard(deck.draw());
     
    								handIter--;
    							}
     
    						} else {
    							p("Player busts.");
    							break;
    						}
    					}
    				}

    The full code
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
     
     
    public class Blackjack {
    	private static final int DECKS = 6;
    	private static Scanner in = new Scanner(System.in);
     
    	private static Actions actions = new Actions();
     
    	private static int bal = 1000;
     
    	public static void main(String[] args) {
    		Deck deck = new Deck(DECKS);
    		List<Hand> hands;
     
    		boolean playing = true;
     
    		while(playing) {
     
    			p("How many hands would you like to run?");
    			int ans = in.nextInt();
    			p("What would you like the base bet to be?");
    			int b = in.nextInt();
     
    			int[] bets = {(b * 1), (b * 3), (b * 2), (b * 6), (b * 3), (b * 4), (b * 5), (b * 6), (b * 7), (b * 8), (b * 9), (b * 10)};
    			int currBetPos = 0;
     
    			//Begin automation
     
    			game:
    			for (int iter = 1; iter <= ans; iter++) {
    				deck.shuffle();
    				hands = new ArrayList<Hand>();
     
    				p("Current balance is: " + bal);
     
    				while (bal - bets[currBetPos] < 0) {
    					currBetPos--;
     
    					if (currBetPos < 0) break game;
    				}
     
    				int bet = bets[currBetPos++];
    				bal -= bet;
     
    				p("Player bets " + bet + ".  Balance is now " + bal);
     
    				int origBet = bet;
     
    				if (currBetPos > bets.length) 
    					currBetPos = 0;
     
     
    				//Initial Deal
    				Hand pHand = new Hand();
    				Hand dHand = new Hand();
     
    				pHand.addCard(deck.draw());
    				dHand.addCard(deck.draw());
    				pHand.addCard(deck.draw());
    				dHand.addCard(deck.draw());
     
    				hands.add(pHand);
    				hands.add(dHand);
     
    				//Play main player hand
     
    				hands:
    				for (int i = 0; i < hands.size(); i++) {
    					if (i == 1) 
    						continue;
     
    					int handIter = 0;
     
    					while (true) {
    						handIter++;
    						System.out.println(handIter);
     
    						p("Player has: " + hands.get(i) + " for a value of " + hands.get(i).getValue() + ". Dealer shows " + hands.get(1).getCard(1).getName());
     
    						if (hands.get(i).getValue() <= 21) {
    							String action = actions.getAction(hands.get(i), hands.get(1));
     
    							if (action.equals("HIT")) {
    								p("Player hits.");
     
    								hands.get(i).addCard(deck.draw());
    							} else if (action.equals("STAY")) {
    								p("Player stays.");
     
    								break;
    							} else if (action.equals("DOUBLE") && handIter == 1) {
    								p("Player doubles.");
     
    								if (bal - origBet >= 0) {
    									bal -= origBet;
    									bet += origBet;
     
    									hands.get(i).addCard(deck.draw());
     
    									break;
    								} else {
    									hands.get(i).addCard(deck.draw());
    								}
    							} else if (action.equals("SPLIT") && handIter == 1) {
    								if (bal - origBet < 0) {
    									p("Player is out of money!");
    									break game;
    								}
     
    								p("Player splits.");
     
    								hands.add(new Hand());
     
    								hands.get(hands.size() - 1).addCard(hands.get(i).removeCard(1));
    								hands.get(hands.size() - 1).addCard(deck.draw());
     
    								hands.get(i).addCard(deck.draw());
     
    								handIter--;
    							}
     
    						} else {
    							p("Player busts.");
    							break;
    						}
    					}
    				}
     
    				//Play dealer hand
     
    				while (true) {
    					if (hands.get(1).getValue() < 17) {
    						hands.get(1).addCard(deck.draw());
    					} else {
    						break;
    					}
    				}
     
    				p("Dealer has " + hands.get(1).toString() + " for " + hands.get(1).getValue());
     
    				//Check outcome
     
    				for (int i = 0; i < hands.size(); i++) {
    					if (i == 1) continue;
     
    					if (hands.get(i).getValue() > 21) {
    						p("You bust.");
    						continue;
    					}
     
    					if (hands.get(1).checkBlackjack()) {
     
    						if (hands.get(i).checkBlackjack()) {
    							bal += origBet;
     
    							p("Both had blackjack.  Player pushes.");
    						}
     
    					} else if (hands.get(i).checkBlackjack()) {
    						bal += origBet;
    						bal += (origBet * 1.5);
     
    						p("Player had blackjack.");
    					} else if (hands.get(i).getValue() > hands.get(1).getValue()) {
    						bal += (origBet * 2);
    						p("Player beat dealer.");
    					} else {
    						p("Dealer beat player.");
    					}
    				}
    			}
    		}
    	}
     
    	private static void p(String s) {
    		System.out.println(s);
    	}
     
    	private static void p(int s) {
    		System.out.println(s);
    	}
    }

    And finally the rest of the classes:
    Actions.java
    public class Actions {
    	private static final String[][] actions = {
    		//              2        3          4        5          6        7         8         9         10         Ace
    		/* 12      */{"HIT   ", "HIT   ", "STAY  ", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "}, //Index 0
    		/* 13      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 14      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 15      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 16      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 17      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  "},
     
    		/* 8       */{"HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "}, //Index 6
    		/* 9       */{"HIT   ", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 10      */{"DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   ", "HIT   "},
    		/* 11      */{"DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   "},
     
    		/* 2 - 2   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "HIT   ", "HIT   ", "HIT   ", "HIT   "}, //Index 10
    		/* 3 - 3   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 4 - 4   */{"HIT   ", "HIT   ", "HIT   ", "SPLIT ", "SPLIT ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 5 - 5   */{"DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   ", "HIT   "},
    		/* 6 - 6   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 7 - 7   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* 8 - 8   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT "},
    		/* 9 - 9   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "STAY  ", "SPLIT ", "SPLIT ", "STAY  ", "STAY  "},
    		/* 10 - 10 */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  "},
    		/* A - A   */{"SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT ", "SPLIT "},
     
    		/* A2 - A3 */{"HIT   ", "HIT   ", "HIT   ", "DOUBLE", "DOUBLE", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "}, //Index 20
    		/* A4 - A5 */{"HIT   ", "HIT   ", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* A6      */{"HIT   ", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "HIT   ", "HIT   ", "HIT   ", "HIT   ", "HIT   "},
    		/* A7      */{"STAY  ", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "STAY  ", "STAY  ", "HIT   ", "HIT   ", "HIT   "},
    		/* A8      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  "},
    		/* A9      */{"STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  ", "STAY  "}
    	};
     
    	public static String getAction(Hand player, Hand dealer) {
    		int playerIndex = 0;
    		int dealerIndex = 0;
     
    		//Find player index
    		switch (player.getValue()) {
    			case 12:
    				playerIndex = 0;
    				break;
    			case 13:
    				playerIndex = 1;
    				break;
    			case 14:
    				playerIndex = 2;
    				break;
    			case 15:
    				playerIndex = 3;
    				break;
    			case 16:
    				playerIndex = 4;
    				break;
    			case 17:
    				playerIndex = 5;
    				break;
    			case 8:
    				playerIndex = 6;
    				break;
    			case 9:
    				playerIndex = 7;
    				break;
    			case 10:
    				playerIndex = 8;
    				break;
    			case 11:
    				playerIndex = 9;
    				break;
    		}
     
    		if (player.getValue() >= 17 && player.getValue() <=21) {
    			playerIndex = 25;
    		}
     
    		if (player.getHandSize() == 2) {
    			int plyrVal = player.getCard(0).getValue();
    			int plyrVal1 = player.getCard(1).getValue();
     
    			String plyrName = player.getCard(0).getName();
    			String plyrName1 = player.getCard(1).getName();
     
    			if (plyrVal == 2 && plyrVal1 == 2) {
    				playerIndex = 10;
    			} else if (plyrVal == 3 && plyrVal1 == 3) {
    				playerIndex = 11;
    			} else if (plyrVal == 4 && plyrVal1 == 4) {
    				playerIndex = 12;
    			} else if (plyrVal == 5 && plyrVal1 == 5) {
    				playerIndex = 13;
    			} else if (plyrVal == 6 && plyrVal1 == 6) {
    				playerIndex = 14;
    			} else if (plyrVal == 7 && plyrVal1 == 7) {
    				playerIndex = 15;
    			} else if (plyrVal == 8 && plyrVal1 == 8) {
    				playerIndex = 16;
    			} else if (plyrVal == 9 && plyrVal1 == 9) {
    				playerIndex = 17;
    			} else if (plyrVal == 10 && plyrVal1 == 10) {
    				playerIndex = 18;
    			} else if (plyrName.equals("Ace") && plyrName1.equals("Ace")) {
    				playerIndex = 19;
    			} else if ((plyrName.equals("Ace") && (plyrVal1 == 2 || plyrVal1 == 3)) || (plyrName1.equals("Ace") && (plyrVal == 2 || plyrVal == 3))) {
    				playerIndex = 20;
    			} else if ((plyrName.equals("Ace") && (plyrVal1 == 4 || plyrVal1 == 5)) || (plyrName1.equals("Ace") && (plyrVal == 4 || plyrVal == 5))) {
    				playerIndex = 21;
    			} else if ((plyrName.equals("Ace") && plyrVal1 == 6) || (plyrName1.equals("Ace") && plyrVal == 6)) {
    				playerIndex = 22;
    			} else if ((plyrName.equals("Ace") && plyrVal1 == 7) || (plyrName1.equals("Ace") && plyrVal == 7)) {
    				playerIndex = 23;
    			} else if ((plyrName.equals("Ace") && plyrVal1 == 8) || (plyrName1.equals("Ace") && plyrVal == 8)) {
    				playerIndex = 24;
    			} else if ((plyrName.equals("Ace") && plyrVal1 == 9) || (plyrName1.equals("Ace") && plyrVal == 9)) {
    				playerIndex = 25;
    			}
     
    		}
     
    		//Find dealer index
    		if (dealer.getCard(1).getValue() == 2) {
    			dealerIndex = 0;
    		} else if (dealer.getCard(1).getValue() == 3) {
    			dealerIndex = 1;
    		} else if (dealer.getCard(1).getValue() == 4) {
    			dealerIndex = 2;
    		} else if (dealer.getCard(1).getValue() == 5) {
    			dealerIndex = 3;
    		} else if (dealer.getCard(1).getValue() == 6) {
    			dealerIndex = 4;
    		} else if (dealer.getCard(1).getValue() == 7) {
    			dealerIndex = 5;
    		} else if (dealer.getCard(1).getValue() == 8) {
    			dealerIndex = 6;
    		} else if (dealer.getCard(1).getValue() == 9) {
    			dealerIndex = 7;
    		} else if (dealer.getCard(1).getValue() == 10) {
    			dealerIndex = 8;
    		} else if (dealer.getCard(1).getName().equals("Ace")) {
    			dealerIndex = 9;
    		}
     
    		System.out.println("(" + playerIndex + "," + dealerIndex + ")");
     
    		return actions[playerIndex][dealerIndex].trim();
    	}
     
     
    }

    Card.java
    public class Card {
    	private String name;
    	private int value;
     
    	public Card(String n, int v) {
    		name = n;
    		value = v;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public int getValue() {
    		return value;
    	}
     
    	public String toString() {
    		return name;
    	}
    }

    Deck.java
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
     
     
    public class Deck {
    	private List<Card> cards;
    	private List<Card> cardsback = new ArrayList<Card>();
     
    	public Deck() {
    		String names[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
     
    		for (int j = 0; j < names.length; j++) {
    			int val = j + 1;
     
    			if (val > 10)
    				val = 10;
     
    			if (names[j].equals("Ace")) 
    				val = 11;
     
    			for (int k = 0; k < 4; k++)
    				cardsback.add(new Card(names[j], val));
    		}
     
    		shuffle();
    	}
     
    	public Deck(int d) {
    		String names[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
     
    		for (int i = 0; i < d; i++) {
    			for (int j = 0; j < names.length; j++) {
    				int val = j + 1;
     
    				if (val > 10)
    					val = 10;
     
    				if (names[j].equals("Ace")) 
    					val = 11;
     
    				Card c = new Card(names[j], val);
     
    				for (int k = 0; k < 4; k++)
    					cardsback.add(c);
    			}
    		}
     
    		shuffle();
    	}
     
    	public List<Card> getDeck() {
    		return cards;
    	}
     
    	public void shuffle() {
    		cards = new ArrayList<Card>();
    		cards.addAll(cardsback);
     
    		Collections.shuffle(cards);
    	}
     
    	public Card draw() {
     
    		Card c = new Card(cards.get(0).getName(), cards.get(0).getValue());
    		cards.remove(0);
     
    		return c;
    	}
     
    	public String toString() {
    		String s = "";
     
    		for (int i = 0; i < cards.size(); i++) {
    			s = s + cards.get(i) + ", ";
    		}
     
    		s = s.substring(0, s.length() - 2);
     
    		return s;
     
    	}
    }

    Hand.java
    import java.util.ArrayList;
     
     
    public class Hand {
    	private ArrayList<Card> cards;
    	private int value;
     
    	public Hand() {
    		cards = new ArrayList<Card>();
    	}
     
    	public Hand(Hand h) {
    		cards.addAll(h.getHand());
    		getValue();
    	}
     
    	public Hand(ArrayList c) {
    		cards = c;
    	}
     
    	public Card removeCard(int index) {
    		Card c = cards.get(index);
    		cards.remove(index);
    		return c;
    	}
     
    	public void addCard(Card c) {
    		cards.add(c);
    		value += c.getValue();
    	}
     
    	public Card getCard(int pos) {
    		return cards.get(pos);
    	}
     
    	public int getHandSize() {
    		return cards.size();
    	}
     
    	public int getValue() {
    		int nv = 0;
    		for (int i = 0; i < cards.size(); i++) {
    			nv += cards.get(i).getValue();
    		}
     
    		while (nv > 21) {
    			checkAce();
    		}
     
    		value = nv;
     
    		return value;
    	}
     
    	public ArrayList getHand() {
    		return cards;
    	}
     
    	public void checkAce() {
    		for (int i = 0; i < cards.size(); i++) {
    			if (cards.get(i).getName().equals("Ace")) {
    				value -= 10;
    			}
    		}
    	}
     
    	public boolean checkBlackjack() {
    		if (getHandSize() == 2 && getValue() == 21) { 
    			return true;
    		}
     
    		return false;
    	}
     
    	public String toString() {
    		String s = "";
    		for (int i = 0; i < cards.size(); i++) {
    			s = s + cards.get(i) + ", ";
    		}
     
    		s = s.substring(0, s.length() - 2);
     
    		return s;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: while loop just stops looping

    You're the first person I've seen use the break label; statement. It's a crutch. It's legal, but it's still a crutch to prop up a questionable design.

    Since the while ( true ) loop is exiting unexpectedly, examine every possible exit and determine why one of them is causing the exit when you think it shouldn't be. Add print statements if you need to that clearly indicate when the while loop is being exited. It's also possible that the while ( true ) loop is consuming a system or JVM resource that causes the program to exit. I don't see it, but it's possible.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    It isn't exactly "exiting" in the traditional sense. The program keep running, but it stops outputting anything. No crashes, no errors, nothing. Eclipse says it is running by the lack of output says it is not.

    As for the break label;, how would you recommend I do it? I need to have loops in loops, and sometimes I need to end higher loops then the one I am in.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    So? That is a different website? That thread was not getting me anywhere so I posted it here too..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    It's considerate to say where a problem has been posted so everyone that is trying to help can see what has already been done so they don't need to waste time repeating things already discussed.

    That thread was not getting me anywhere
    Several suggestions were made on that other site. Did you try any of them?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    I did, but it still didn't work. Same problem.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    it still didn't work. Same problem.
    Yes, those suggestions were for doing some debugging to find the problem. They would NOT fix the problem.

    If the code now executes the same way every time (no random input and with a fixed deck),
    now you need to add some println statements to show where the code is executing and how the values of the variables are changed as the code executes. This is a iterative process that can take several trials to narrow down where the bug is.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    The loop is filled with printlns.. I said on the other one I was just using p() instead to make it shorter. I fail to see how adding more will do anything.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    I fail to see how adding more will do anything.
    How many years of programming experience do you have?

    Can you copy the full contents of the console from when you execute the program that has all the output from the print statements you currently have?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    How many years of experience I have is absolutely irrelevant here.

    This is everything the console outputs, as I stated in my original post.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    I suggest that you "fail to see" because you are a beginner.
    the console outputs, as I stated in my original post.
    Then you need to add LOTS more printlns that show where the code is executing and what the values are as it executes.

    What is the last statement executed before the program "stops"?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    I am not a "beginner", but thanks. Perhaps instead of questioning my experience, you could help me see.

    The program stops at different points every time. The given output is an example, but it never stops at the same point.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    The program stops at different points every time.
    It wouldn't if you had done what was suggested on the other site: Get rid of the shuffle. Have fixed user input.

    What are the last statements executed for each time it "stops"?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    I don't think you are understanding my properly.

    If the deck is A, B, C, D, E, and F and I want to print out the shuffled deck, I don't mean it outputs:

    C, D, E, F
    then
    B, C, A, D
    etc.

    I mean it might print out:
    A, B, D
    D, A, C, E
    F, B

    With each line being a different time the program ran. It isn't printing out everything that it SHOULD, regardless of order. Does that make sense?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    Is that related to the "it stops" problem? Have you found the last statement that executes before the program "stops"?

    With each line being a different time the program ran
    Changing the data every time the program executes sometimes makes it hard to find a problem. If the program always uses the same data, it will be easier.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    My problem has nothing to do with the data. Each hand has different data, but not all of the hands are running. A different number run each time before I run into my problem. I couldn't care less about the data in each hand, right now I just need every hand to run.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    I thought you wanted to find why the program "stops"?
    If the program is not stopping now, can you explain what the current problem is?

    A different number run each time before I run into my problem.
    That is because the shuffle() method changes the data every time the program executes. Its a moving target. Comment out the call to Collections.shuffle() and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    Look, ignore everything I have, alright?

    If I came to you and said "Hey, I have this while loop that will sometimes freeze the program. The program doesn't actually stop, but there is no more output and nothing past the while loop runs. What might cause something like this to occur?", what would you say?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    Start adding some println statements to find where it is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Kewish (November 24th, 2013)

  22. #21
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    Ok, obviously you are losing me here. Would you be so kind as to show me how you would implement all of these printlns? Because I see no place that I might add more.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop just stops looping

    I see no place that I might add more.
    If you think a while() loop is going forever, add a println() inside of all the while loops.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Junior Member
    Join Date
    Feb 2014
    Location
    California
    Posts
    9
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop just stops looping

    Hey dstar5, I don't know if you ever solved this problem or if you're even still interested. I just joined the site and have been poking around to see what problems others are (or were) dealing with and I think I understand what's going on here. Have you considered looking outside of your Blackjack class for places to add additional traces? I think you are encountering an infinite loop, depending upon what card(s) are dealt.

  25. #24
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: while loop just stops looping

    Hi dstar5 obviously there is a bug in your algorithm (a programmer's error maybe).
    you are trying to solve it using prints.
    Suggestion:
    if your using any IDE, well, I think using its debug mode will make you solve your problem easily, without sweat, just put a break point on each break; statement, and in each conditional statements. and i'm sure you are going to trace where it stops. no sweat if your using IDE.

    but if you are not using IDE, you can use the debug mode of java. using this command: jdb <class> (debug mode of java) instead of java <class.
    And please be calm and polite to others that tried to help you. as what I see in your profile, you are intermediate level in java. that kind of bug is easy to trace and usually a big problem for beginners not for intermediate level.

Similar Threads

  1. While loop, help with looping.
    By Ancharius in forum Loops & Control Statements
    Replies: 2
    Last Post: October 29th, 2013, 09:46 AM
  2. Why is my while loop not looping?
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 24th, 2013, 09:37 PM
  3. Code just stops when string is initialised
    By Volantary in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 10th, 2012, 02:55 PM
  4. Ant Executes and Stops After a Certain Task
    By DanielPros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 10:46 AM
  5. While loop stops.
    By MrFish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2010, 06:40 PM