0

Kinda new to Java here. I want to detect the words "winner" or "loser" in my print return statements belonging to makeBet() in my RouletteGame class from playRound() in class RouletteTester. I'm trying to create boolean statements based on which word was detected. Is there any way to do this?

Here is my method from the class RouletteGame:

public String makeBet(int betAmount, String betType) {
    String result = this.wheel.spin();

    if ((betAmount > 0) || (betAmount < credits)) {

        // Determines results for color bets
        if (betType == "red" || betType == "black") {
            this.wheel.getColor(result);
            if (this.wheel.getColor(result) == "red") {
                this.credits += betAmount;
                return this.wheel.getColor(result) + "- winner";
            }
            else if (this.wheel.getColor(result) != "red") {
                this.credits -= betAmount;
                return this.wheel.getColor(result) + " - loser";
            }
            if (this.wheel.getColor(result) == "black") {
                this.credits -= betAmount;
                return this.wheel.getColor(result) + " - winner";
            }
            else if (this.wheel.getColor(result) != "black") {
                this.credits -= betAmount;
                return this.wheel.getColor(result) + " - loser";
            }
        }

        // Determines results for parity bets
        if (betType == "even" || betType == "odd") {
            this.wheel.getParity(result);
            if (this.wheel.getParity(result) == "even") {
                this.credits += betAmount;
                return this.wheel.getParity(result) + "- winner";
            }
            else if (this.wheel.getParity(result) != "even") {
                this.credits -= betAmount;
                return this.wheel.getParity(result) + " - loser";
            }
            if (this.wheel.getParity(result) == "odd") {
                this.credits -= betAmount;
                return this.wheel.getParity(result) + " - winner";
            }
            else if (this.wheel.getParity(result) != "odd") {
                this.credits -= betAmount;
                return this.wheel.getParity(result) + " - loser";
            }
        }

        // Determines results for a bet on a certain number
        int intResult = Integer.parseInt(result);
        int intBet = Integer.parseInt(betType);
        if (intBet == intResult) {
            this.credits += 35*betAmount;
            return result + " - winner";
        }
        else {
            this.credits -= betAmount;
            return result + " - loser";
        }
    }
    else if (betAmount < 0) {
        this.credits = 0;
    }
    else if (betAmount > credits) {
        betAmount = this.credits;
    }
    return "Please spin again";

And I am trying to reference my return statements in makeBet() from playRound() in class RouletteTester (below). After a loss (i.e., if the string return statement in makeBet() contains "loser"), I would like to double the bet amount, and after a win (if it contains the word, "winner"), I would like to go back to the original bet amount.

public static int playRound(String betType, int betAmount) {
    int startAmount = betAmount * RouletteTester.SPINS_PER_ROUND;

    RouletteGame game = new RouletteGame();
    game.addCredits(startAmount);
    for (int roundNum = 0; roundNum < RouletteTester.SPINS_PER_ROUND; roundNum++) {
        if (game.checkCredits() > 0) {
            int nextBet = Math.min(betAmount, game.checkCredits());
            game.makeBet(nextBet, betType);
        }
    }
    return game.checkCredits() - startAmount;
azw901
  • 1
  • 3
  • 1
    Welcome to StackOverflow. Your question as it is is awfully unprecise. Please take a minute to log at [how to ask a question](http://stackoverflow.com/help/mcve) and improve your question. – hotzst Jul 10 '16 at 19:21
  • Show your code please. – sinclair Jul 10 '16 at 20:20
  • Please note how to compare strings in java: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – sinclair Jul 13 '16 at 09:26

2 Answers2

0

Use string.contains("winner") . I think thats what you need. It returns a boolean.

Akashdeep Saluja
  • 2,959
  • 8
  • 30
  • 60
0

As the first commentator said, the question is pretty unprecise. Please make you question a bit more precise and/or provide some code snippet.

In any way, if you want to check whether a string contains some character sequence you can use the .contains() method.

Otherwise you can use the .equals() method to check whether you string is equal to something else.

Based on this you can your class/method check the returned value of the other method for the string you would like to.

akortex
  • 5,067
  • 2
  • 25
  • 57