2

I'm making a simple coin toss game, and I wrote several methods to call and make my main class short and simple. After the game is played once, the first If/Else statement to ask users for input is jumping right to the Else statement without prompting for input.

package cointoss;
import java.util.Random;
import java.util.Scanner;
public class Game {
int money;
int result;
int bet;

Random rn = new Random();
Scanner in = new Scanner(System.in);

String playerPick;
String aResult;

public void setMoney(int a)
{
    money = a;
}

public int getMoney()
{
    return money;
}

public void getBet()
{
    System.out.println("How much would you like to bet?");
    bet = in.nextInt();
    do{
    if(bet > money)
    {
        System.out.println("You cannot bet more than you have!");
        System.out.println("You have bet " + (bet - money) + " too many coins.");
        continue;
    }
    else
        System.out.println("You have bet " + bet + " coins.");
    }
    while(bet > money);
}
public void getInput()
{


    System.out.println("Pick Heads or Tails");
    playerPick = in.nextLine();
    playerPick.toLowerCase();

    if(playerPick.contains("heads"))
        playerPick ="heads";
    else if(playerPick.contains("tails"))
        playerPick ="tails";
    else
        System.out.println("Invalid Selection");
    }
public void flipCoin()
{
    result = rn.nextInt(2);
    if(result == 0)
    {
        aResult = "heads";
    }
    else
        aResult = "tails";
}

public void checkResult()
{
    if(playerPick.equals(aResult))
    {
        System.out.println("You have won!");
        money += bet;
        System.out.println("You now have " + money + " coins");

    }
    else{
        System.out.println("You have lost!");
        money -= bet;
        System.out.println("You now have " + money + " coins");
    }
}
}

My Tester Class:

package cointoss;

public class GameTest {
public static void main(String[] args)
{
    Game coinToss = new Game();
    coinToss.setMoney(100);

    while(coinToss.getMoney() > 0)
    {

    coinToss.getInput();
    coinToss.getBet();
    coinToss.flipCoin();
    coinToss.checkResult();
    }
}
}
gm95
  • 802
  • 2
  • 10
  • 16

4 Answers4

7

The method toLowerCase() does not change the contents of the string; String is an immutable class in Java. The toLowerCase() method returns the result. You need to change

playerPick.toLowerCase();

to

playerPick = playerPick.toLowerCase();
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 1
    You could even save some space by writing playerPick = in.nextLine().toLowerCase(); and the lowercase value would be saved in variable playerPick. – La-comadreja Nov 04 '13 at 22:18
  • Ideally, you should not have the game logic deal in strings anyway - make an enum and only use a single string check to set that. – Southpaw Hare Nov 04 '13 at 22:20
  • Use `System.out.println("Invalid Selection: " + playerPick);` to determine what's really coming through the input. – rgettman Nov 04 '13 at 22:22
  • Thank you for the help with toLowerCase. Going to make an enum and a switch statement for the logic now, it seems more "logical" ;) – gm95 Nov 04 '13 at 22:34
1

Your problem is that you are not reinitializing "in" as a new Scanner every time you run the tester loop. The single scanner reads a line of input and accepts that as the full answer, without acknowledging that there could be further input.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
1

The problem is that when the user enters a line, the input buffer will contain characters followed by a "newline" (end-of-line) character. When you use nextInt, the Scanner will find and skip over an integer. But it won't skip over the end-of-line. So when you next call nextLine in getInput(), it will then find what's left of the previous line, i.e. an empty string, and return that. Some things you'll need to do:

(1) In getBet, add in.nextLine() at the end of the method, to skip past the end-of-line. nextLine will return a string but you can ignore it. See also Scanner issue when using nextLine after nextXXX

(2) getInput needs to have a loop so that if the user enters an invalid input, you go back and ask him to enter a valid string. Otherwise, it will display "Invalid Selection" but then ask for a bet, which isn't what you want.

(3) See the other answers with regard to toLowerCase.

Community
  • 1
  • 1
ajb
  • 31,309
  • 3
  • 58
  • 84
0

When you use

playerPick.toLowerCase();

It does nothing because the value is not being assigned to anything. In order to change a value of an object you must assign a value to it, as below:

 playerPick = Pick.toLowerCase();

This assigns the value, rather than calling an empty method

Hope this helps :)

Joe
  • 528
  • 5
  • 19