Results 1 to 7 of 7
- 07-02-2012, 07:52 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Allowing for output of decimals using JOptionPane
So I'm really new to java and just trying to gain experience before I start school, I'm writing simple programs and trying to get more and more complex. I've come across an issue that i'm not sure how to resolve. Here is my code:
import javax.swing.JOptionPane;
public class LOLcharchance {
public static void main(String args[]){
String champion = JOptionPane.showInputDialog("What champion do you want on your team?");
String p1 = JOptionPane.showInputDialog("How many champions does player one have?");
String p2 = JOptionPane.showInputDialog("How many champions does player two have?");
String p3 = JOptionPane.showInputDialog("How many champions does player three have?");
String p4 = JOptionPane.showInputDialog("How many champions does player four have?");
String p5 = JOptionPane.showInputDialog("How many champions does player five have?");
String answer = JOptionPane.showInputDialog("How many players own " + champion + "?" );
int champ = Integer.parseInt(champion);
int pl1 = Integer.parseInt(p1);
int pl2 = Integer.parseInt(p2);
int pl3 = Integer.parseInt(p3);
int pl4 = Integer.parseInt(p4);
int pl5 = Integer.parseInt(p5);
int answ = Integer.parseInt(answer);
float ans = (pl1 + pl2 + pl3 + pl4 + pl5) / champ;
JOptionPane.showMessageDialog(null, "ARAM champion % " + ans, "ARAM", JOptionPane.PLAIN_MESSAGE);
The problem I get is when I finish the last inputs I get an error if the answer needs to output a decimal, here is the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Soraka"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at LOLcharchance.main(LOLcharchance.java:17)
so if the answer is even and gives me 5% i'll get an output, if it tries to handle a decimal like 5.5% i get the above error.
Any help would be much appreciated! Thanks! (and remember i'm super new to java so java for idiots style would be good ) :)
- 07-02-2012, 08:33 PM #2
Re: Allowing for output of decimals using JOptionPane
You're using Integer. Use Double (or Float) for parsing decimals.
But I don't think your error is what you think it is. The error message is saying that it's trying to parse "Soraka" which is not an integer or a decimal.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 07-02-2012, 08:49 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Allowing for output of decimals using JOptionPane
Okay, so how do I fix that error then? Thats another question i've struggled finding the answer to. wouldn't that be considered a string? But I cant do string champ as a variable, what would I use other then int?
-
Re: Allowing for output of decimals using JOptionPane
The solution is to only enter numeric data, not some String such as "Soraka".
- 07-02-2012, 09:22 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Allowing for output of decimals using JOptionPane
But I want to use that input slot for string not numeric data. Then what?
-
Re: Allowing for output of decimals using JOptionPane
- 07-03-2012, 04:09 AM #7
Similar Threads
-
Preventing XSS but allowing certain characters like apostrophe,!,"
By maninder in forum Java ServletReplies: 3Last Post: 02-02-2012, 09:50 AM -
Overriding KeyListener but still allowing input?
By Zepher in forum New To JavaReplies: 6Last Post: 02-14-2011, 06:31 AM -
Allowing only hex values in a textfield
By elsanthosh in forum AWT / SwingReplies: 3Last Post: 07-20-2010, 09:31 AM -
Allowing only numeric values in a TextField
By Java Tip in forum Java TipReplies: 0Last Post: 03-01-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks