Reputation: 842
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();
}
}
}
Upvotes: 2
Views: 197
Reputation: 31699
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
.
Upvotes: 1
Reputation: 5755
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.
Upvotes: 1
Reputation: 538
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 :)
Upvotes: 0
Reputation: 178363
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();
Upvotes: 7