Question

I'm having trouble with my code. When I run it, the program keeps running, it won't stop. I'm not sure what I did wrong. I've gone over it but I haven't been able to figure it out. Someone please help!

public void play() {
    /**
    * The main algorithm for single player poker game
    *
    * Steps: showPayoutTable()
    *
    * ++ show balance, get bet verify bet value, update balance reset deck,
    * shuffle deck, deal cards and display cards ask for position of cards
    * to keep get positions in one input line update cards check hands,
    * display proper messages update balance if there is a payout if
    * balance = O: end of program else ask if the player wants to play a
    * new game if the answer is "no" : end of program else :
    * showPayoutTable() if user wants to see it goto ++
    */
    // implement this method!

    Scanner input = new Scanner(System.in);
    List<Card> keepCard = new ArrayList<Card>();
    int counter = 0;
    boolean newGame = true;
    boolean rightBet = false;
    while (newGame) {
        oneDeck.shuffle();
        showPayoutTable();

        System.out.println("Balance:" + balance + "\n");

        while (!rightBet) {
            System.out.print("Enter bet:");
            bet = Integer.parseInt(input.nextLine());

            if (bet > balance) {
                System.out.println("insufficient fund!");
                rightBet = false;
            } else {
                rightBet = true;
            }
        }

        balance = balance - bet;

        try {
            currentHand = oneDeck.deal(5);
        } catch (PlayingCardException e) {
            System.out.println("Exception dealing a new hand" + e.getMessage());
        }

        System.out.println("" + currentHand.toString());
        System.out.print("Enter positions to keep:");

        if (input.hasNext()) {
            String s = input.nextLine();
            if (!(input.nextLine() == "0")) {
                input = new Scanner(s);
                input = input.useDelimiter("\\s*");

                while (input.hasNext()) {
                    keepCard.add(currentHand.get((input.nextInt()) - 1));
                    counter++;
                }
            }
        }
        currentHand = keepCard;
        try {
            currentHand.addAll(oneDeck.deal(5 - counter));
        } catch (PlayingCardException e) {
            System.out.println("Exception dealing the second hand" + e.getMessage());
        }

        System.out.println("" + currentHand.toString());
        checkHands();
        System.out.println("Your balance: " + balance + " you want another game y/n ?");

        if (input.hasNext()){
            String s = input.nextLine(); 
            if (balance == 0) {
                newGame = false;
                break;
            }

            if (s == "y") {
                newGame = true;
            } else {
                newGame = false;
            }

            System.out.println("Want to see payout table ? (y/n)");

            if (input.hasNext()) {
                s = input.nextLine();

                if (s == "y") {
                    showPayoutTable();
                }
                oneDeck.reset();

            }
            System.out.println("Bye!");
        }
    }
}
Was it helpful?

Solution

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top