Results 1 to 8 of 8
  1. #1
    timtim123 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default converting string to number for boolean and if/if else with message boxes

    Hello, I searched around over the last day and although I found multiple threads that helped me understand how the code works and such, I didn't seem to find anything I could directly apply to what I'm working on.

    I am trying to get a simple rock, paper, scissors program working with an input dialogue box to enter your choice (this works fine) and then message dialogue boxes for the results (Not working). I understand that you have to convert a string to an int in order to use mathematical equations or Boolean expressions, but I can't seem to get it working with if and if else in the mix. Below is what I'm working with. For now, I'm just trying to get the "Invalid" portion working which I assume I can re-use for the other portions. Everything works fine in the command prompt output, just need to get displays in message boxes ---
    Java Code:
    import javax.swing.JOptionPane;
    
    public class RockPaperScissors2 {
    public static void main(String[] args) {
    
    //Random number for computer choice
    int compPick = (int)(Math.random() * 3 );
    
    //Enter user pick
    String Choice = JOptionPane.showInputDialog (
    "Pick Rock, Paper, or Scissors: (0 = Scissors, 1 = Rock, 2 = Paper): ");
    
    
    String output = "Computer picks: " + compPick;
    JOptionPane.showMessageDialog(null, output);
    
    // Invalid Entry
    
    int Invalid = Integer.parseInt(Invalid);
    if (Choice >= 3);
    String Invalid = "Invalid Entry";
    JOptionPane.showMessageDialog(null, "Invalid");
    
    
    //Draw result
    else if (Choice == compPick)
    System.out.println ("Draw");
    
    //Winning results
    else if (Choice == 0 && compPick == 2)
    System.out.println("Scissors beats Paper. You Win!");
    
    else if (Choice == 1 && compPick == 0)
    System.out.println("Rock beats Scissors. You Win!");
    
    else if (Choice == 2 && compPick == 1)
    System.out.println("Paper beats Rock. You Win!");
    
    //Losing results
    else if (Choice == 0 && compPick == 1)
    System.out.println("Scissors loses to Rock. You Lose.");
    
    else if (Choice == 1 && compPick == 2)
    System.out.println("Rock loses to Paper. You Lose.");
    
    else if (Choice == 2 && compPick == 0)
    System.out.println("Paper loses to Scissors. You Lose.");
    
    }
    }
    Last edited by Eranga; 01-06-2012 at 03:44 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: converting string to number for boolean and if/if else with message boxes

    On the following line, where you define and initialize the variable Invalid?

    Java Code:
    int Invalid = Integer.parseInt(Invalid);

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: converting string to number for boolean and if/if else with message boxes

    And also please use the code tags when you are posting code segments again to the forum. Unformated codes are really hard to read at once.

  4. #4
    timtim123 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: converting string to number for boolean and if/if else with message boxes

    Thanks for the reply, unfortunately, I'm not following though. I'm trying to have all of the results display in message boxes. From what I understand, I have to convert the string to a number in order to do this, right? Am I converting the string "Choice" to an int for this to work? Below is the program as it sits currently. All of the outputs are displaying in message boxes up until it gets to the Invalid Entry point and beyond, I guess because this is where the boolean expressions come in and is failing since it isn't converted to an int?

    Java Code:
    import javax.swing.JOptionPane;
    
    public class RockPaperScissors2 {
    public static void main(String[] args) {
    
    //Random number for computer choice
    int compPick = (int)(Math.random() * 3 );
    
    //Enter user pick
    String Choice = JOptionPane.showInputDialog (
    "Pick Rock, Paper, or Scissors: (0 = Scissors, 1 = Rock, 2 = Paper): ");
    
    
    String output = "Computer picks: " + compPick;
    JOptionPane.showMessageDialog(null, output);
    
    
    // Invalid Entry
    
    if (Choice >= 3)
     	System.out.println ("Invalid Entry");
    
    
    //Draw result
    else if (Choice == compPick)
    System.out.println ("Draw");
    
    //Winning results
    else if (Choice == 0 && compPick == 2)
    System.out.println("Scissors beats Paper. You Win!");
    
    else if (Choice == 1 && compPick == 0)
    System.out.println("Rock beats Scissors. You Win!");
    
    else if (Choice == 2 && compPick == 1)
    System.out.println("Paper beats Rock. You Win!");
    
    //Losing results
    else if (Choice == 0 && compPick == 1)
    System.out.println("Scissors loses to Rock. You Lose.");
    
    else if (Choice == 1 && compPick == 2)
    System.out.println("Rock loses to Paper. You Lose.");
    
    else if (Choice == 2 && compPick == 0)
    System.out.println("Paper loses to Scissors. You Lose.");
    
    }
    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: converting string to number for boolean and if/if else with message boxes

    First of all run the following line of code and see.

    Java Code:
    JOptionPane.showMessageDialog(null, 1, "Title", JOptionPane.INFORMATION_MESSAGE);
            
    JOptionPane.showMessageDialog(null, "One", "Title", JOptionPane.INFORMATION_MESSAGE);

  6. #6
    timtim123 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: converting string to number for boolean and if/if else with message boxes

    I don't understand? What am I supposed to do with those two lines? I'm not trying to have anyone write this for me, but any references or examples that can help me out would be appreciated.

    I put those two lines in my program and tried to compile and just got a few errors that led to more each time I tried to correct

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: converting string to number for boolean and if/if else with message boxes

    Quote Originally Posted by timtim123 View Post
    I don't understand? What am I supposed to do with those two lines? I'm not trying to have anyone write this for me, but any references or examples that can help me out would be appreciated.
    I am not writing code for you. Just give you a guidance. I have not ask you to put those lines into your code and see, just say to run those lines of code and see. Because I want to give an answer to the following question.

    Quote Originally Posted by timtim123 View Post
    I'm trying to have all of the results display in message boxes. From what I understand, I have to convert the string to a number in order to do this, right?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: converting string to number for boolean and if/if else with message boxes

    Initialize a string variable and in each result conditions (if/else if) assign the result into it. Then at the end you can display in a message box. If you pick what I in 5th post, really you don't want to convert the variable into int and so on. Doing that have some advantages, but not necessary.

Similar Threads

  1. Replies: 2
    Last Post: 11-27-2011, 05:11 PM
  2. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  3. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM
  4. Built-in User Popup message boxes ?
    By BobZ in forum AWT / Swing
    Replies: 2
    Last Post: 02-06-2009, 06:23 PM
  5. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •