Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Noob: Coin Toss Simulator (no GUI)

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default 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)
    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
    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
    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;
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,625
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Noob: Coin Toss Simulator (no GUI)

    if(guess == checkSide) {
    You're using the == operator incorrectly. See: Common Java Mistakes: == operator or equals() method

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    bgroenks96 (June 4th, 2011)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,625
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Java Noob: Coin Toss Simulator (no GUI)

    Quote Originally Posted by Norm View Post
    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.
    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)

  7. #6
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Java Noob: Coin Toss Simulator (no GUI)

    Quote Originally Posted by helloworld922 View Post
    You're using the == operator incorrectly. See: Common Java Mistakes: == operator or equals() method
    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)
    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
    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.
    Last edited by bgroenks96; June 4th, 2011 at 10:29 PM. Reason: Forgot a label

Similar Threads

  1. [SOLVED] Java Noob, Jframe in Applet.
    By rLLZORS in forum AWT / Java Swing
    Replies: 2
    Last Post: May 5th, 2011, 10:42 AM
  2. Project Coin and java 7
    By Kerr in forum The Cafe
    Replies: 4
    Last Post: March 27th, 2011, 10:53 AM
  3. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 03:14 AM
  4. Need Help with my hmwk! Java noob!
    By ravij in forum Loops & Control Statements
    Replies: 4
    Last Post: October 7th, 2009, 01:02 AM
  5. JAVA simulator
    By YAS218 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 20th, 2009, 09:57 AM