Results 1 to 7 of 7
  1. #1
    oj7
    oj7 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default 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 ) :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

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

  3. #3
    oj7
    oj7 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Allowing for output of decimals using JOptionPane

    The solution is to only enter numeric data, not some String such as "Soraka".

  5. #5
    oj7
    oj7 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Allowing for output of decimals using JOptionPane

    But I want to use that input slot for string not numeric data. Then what?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Allowing for output of decimals using JOptionPane

    Quote Originally Posted by oj7 View Post
    But I want to use that input slot for string not numeric data. Then what?
    Fine, but then don't try to parse it as a number.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,243
    Rep Power
    19

    Default Re: Allowing for output of decimals using JOptionPane

    Moved from Suggestions and Feedback

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 02-02-2012, 09:50 AM
  2. Overriding KeyListener but still allowing input?
    By Zepher in forum New To Java
    Replies: 6
    Last Post: 02-14-2011, 06:31 AM
  3. Allowing only hex values in a textfield
    By elsanthosh in forum AWT / Swing
    Replies: 3
    Last Post: 07-20-2010, 09:31 AM
  4. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 PM

Posting Permissions

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