Results 1 to 8 of 8
- 01-06-2012, 03:43 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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
- 01-06-2012, 03:46 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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);
- 01-06-2012, 03:47 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-13-2012, 02:20 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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."); } }
- 01-13-2012, 05:05 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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);
- 01-14-2012, 04:53 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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
- 01-16-2012, 03:30 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: converting string to number for boolean and if/if else with message boxes
- 01-16-2012, 03:37 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
converting a given positive integer m to a positional number system in base n
By Renxx in forum New To JavaReplies: 2Last Post: 11-27-2011, 05:11 PM -
Converting whole number into decimal
By jim01 in forum New To JavaReplies: 2Last Post: 09-23-2010, 07:58 PM -
A Number Converting Program!
By WastedxYears in forum New To JavaReplies: 2Last Post: 01-09-2010, 12:47 AM -
Built-in User Popup message boxes ?
By BobZ in forum AWT / SwingReplies: 2Last Post: 02-06-2009, 06:23 PM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks