Java Noob: Coin Toss Simulator (no GUI)
Hey I was just trying to practice some Java programming skills (I have written more advanced stuff than this) but for some reason the code just won't work. It has been written and rewritten several times but always either doesn't work (as in you can't win) or returns exceptions. If you can help, I appreciate it. If not, I will just make it even more basic and remove user interaction.
The program currently contains three classes. Here is the source code for each:
CoinToss.java (Main-Class/Manifest)
Code :
public class CoinToss {
public static void main(String[] args) {
System.out.println("Heads or Tails?");
Coin c = new Coin();
c.chance();
CoinTossHelper check = new CoinTossHelper();
String stringGuess = check.getUserInput("Type Heads or Tails");
String result = c.checkInput(stringGuess);
if(result.equals("Valid")) {
System.out.println(c.getSide() + " You win!");
} else if(result.equals("Invalid")) {
System.out.println(c.getSide() + " You lose.");
} else {
System.out.println("An error has occured.");
}
}
}
Coin.java
Code :
public class Coin {
private String side;
private String result;
String[] choices = {"1","2"};
int length = choices.length;
public void chance() {
int rand1 = (int) (Math.random()*length);
String num = choices[rand1];
if(num == "1") {
side = "Heads";
}
else if(num == "2") {
side = "Tails";
}
}
public String getSide() {
return side;
}
public String checkInput(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
int checkSide = Integer.parseInt(side);
if(guess == checkSide) {
result = "Valid";
} else {
result = "Invalid";
}
return result;
}
}
CoinTossHelper.java
Code :
import java.io.*;
public class CoinTossHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0) return null;
} catch(IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
}
}
Re: Java Noob: Coin Toss Simulator (no GUI)
Can you tell us where this code is? What errors does it generate? Copy and paste the full text here.
Does it give bad output? Copy and paste here what you get as output and add comments to it describing what is wrong with the output and what the output should be.
Re: Java Noob: Coin Toss Simulator (no GUI)
Re: Java Noob: Coin Toss Simulator (no GUI)
@helloworld922 See code: int guess =... and int checkSide = ..
@OP Your usage of variables is confused. Strings with words vs Strings with digits
Add some printlns to show ALL of the variables that are passed to the methods as the first line of each method. For example:
System.out.println("sG=" + stringGuess +"< side=" + side); //<<<
Then look at how the values are used.
Re: Java Noob: Coin Toss Simulator (no GUI)
Quote:
Originally Posted by
Norm
Can you tell us where this code is? What errors does it generate? Copy and paste the full text here.
Does it give bad output? Copy and paste here what you get as output and add comments to it describing what is wrong with the output and what the output should be.
Code :
java CoinToss
Heads or Tails?
Type Heads or Tails Heads
Exception in thread "main" java.lang.NumberFormatException: For input string: "H
eads"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at Coin.checkInput(Coin.java:20)
at CoinToss.main(CoinToss.java:8)
Re: Java Noob: Coin Toss Simulator (no GUI)
Quote:
Originally Posted by
helloworld922
I see. I used your advice (changed == to .equals(STRING)) and came up with this final build. And it works! Thanks for your help!
CoinToss.java (Main)
Code :
public class CoinToss {
public static void main(String[] args) {
System.out.println("Heads or Tails?");
Coin c = new Coin();
c.chance();
CoinTossHelper check = new CoinTossHelper();
String userGuess = check.getUserInput("Type Heads or Tails");
String result = c.checkInput(userGuess);
if(result.equals("Valid")) {
System.out.println(c.getSide() + " You win!");
} else if(result.equals("Invalid")) {
System.out.println(c.getSide() + " You lose.");
} else {
System.out.println("An error has occured.");
}
}
}
Coin.java
Code :
public class Coin {
private String side;
private String result;
String[] choices = {"1","2"};
int length = choices.length;
public void chance() {
int rand1 = (int) (Math.random()*length);
String num = choices[rand1];
if(num == "1") {
side = "Heads";
}
else if(num == "2") {
side = "Tails";
}
}
public String getSide() {
return side;
}
public String checkInput(String userGuess) {
if(userGuess.equals(side)) {
result = "Valid";
} else {
result = "Invalid";
}
return result;
}
}
CoinTossHelper.java was unchanged.