Results 1 to 18 of 18
  1. #1
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Help with JOptionPane

    I am currently receiving an error every time I enter a price into my code. I'm assuming it cannot accept the decimal format but I am unsure of how to go about changing that. Here is what I have so far:

    Java Code:
     import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author me
     */
    public class Tester {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
               
            // TODO code application logic here
            String name;
            int shares;
            int portion1;
            int portion2;
            
            DecimalFormat dec = new DecimalFormat("####.##");
            
            String input = JOptionPane.showInputDialog("Company Name");
            
            name = input;
            
            input = JOptionPane.showInputDialog("Enter Number of Shares");
            
            shares = Integer.parseInt(input);
            
            input = JOptionPane.showInputDialog("Enter Price of Shares in Dollars" + dec.format(portion1));
            
            portion1 = Integer.parseInt(input);
            
            input = JOptionPane.showInputDialog("Enter Price of Shares in Eighths");
            
            portion2 = Integer.parseInt(input);      
            
            
            //create object
            StockPortfolio myStock = new StockPortfolio (name, shares, portion1, portion2);
    I thought the decimal format would help but apparently I misunderstood how to use this. Thank you for your help!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    What error do you encounter? What is the value of portion1 when you pass it into the DecimalFormat?
    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
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "3762.50"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at ClientCode.main(ClientCode.java:37)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 11 seconds)
    That is my exact error. What I need to do is enter the dollar amount and the eighths amount for 1 stock. Dollar amount being 1234.50 and eighths being 30 5/8.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    If that's really the error you're seeing, then this isn't the code you're running.

    I suggest posting an SSCCE that demonstrates exactly what you're doing in as few lines as possible, that way we're all looking at the same thing without any extra or missing code.

    Edit: You're also passing a double String into an Integer function, which isn't going to work.
    Last edited by KevinWorkman; 10-07-2013 at 08:59 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    Yes, it is. If I go back to the original format, delete the decimal format stuff. I get an error message with any sequence of numbers I type with a decimal.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    Quote Originally Posted by aalvarez View Post
    Yes, it is. If I go back to the original format, delete the decimal format stuff. I get an error message with any sequence of numbers I type with a decimal.
    The code that you posted results in a compilation error, not a runtime error.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    Error Message:
    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "9483.34"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at ClientCode.main(ClientCode.java:36)
    Code:
    Java Code:
    import javax.swing.JOptionPane;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author MENI
     */
    public class ClientCode {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
               
            // TODO code application logic here
            String name;
            int shares;
            int portion1;
            int portion2;
            
            
            String input = JOptionPane.showInputDialog("Company Name");
            
            name = input;
            
            input = JOptionPane.showInputDialog("Enter Number of Shares");
            
            shares = Integer.parseInt(input);
            
            input = JOptionPane.showInputDialog("Enter Price of Shares in Dollars");
            
            portion1 = Integer.parseInt(input);
            
            input = JOptionPane.showInputDialog("Enter Price of Shares in Eighths");
            
            portion2 = Integer.parseInt(input);      
            
            
            //create object
            StockPortfolio myStock = new StockPortfolio (name, shares, portion1, portion2);
           
            
        }
    }

  8. #8
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    Quote Originally Posted by KevinWorkman View Post
    If that's really the error you're seeing, then this isn't the code you're running.

    I suggest posting an SSCCE that demonstrates exactly what you're doing in as few lines as possible, that way we're all looking at the same thing without any extra or missing code.

    Edit: You're also passing a double String into an Integer function, which isn't going to work.
    I see. I'm just confused because in order to learn this "lesson" I have to leave all variables as type int. Did I not do this? I'm beyond confused by this assignment.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    Your code now results in a compilation error because you're unnecessarily including the StockPortfolio line.

    But like I said, you seem to be passing a decimal into an Integer function. Integers can't be decimals. Look for the Double or Float classes instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    Quote Originally Posted by aalvarez View Post
    I see. I'm just confused because in order to learn this "lesson" I have to leave all variables as type int. Did I not do this? I'm beyond confused by this assignment.
    If you have to use ints, then you can't enter a double. If you can enter a double, then you can't use ints. Not sure what your requirements are, but it won't work both ways.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    As far as instructions to leave the variables as type int, that is all. In addition to that I have to create a constructor that initializes those variables to arguments passed when the object is created. And then of a course the method to enter and return the values as a decimal. I just don't understand what I am missing and how to go about it if I cannot use a double.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    You can't. You either have to use doubles (or floats), or you can't enter a decimal. I suppose you could read them in as doubles and then cast to int, but that would just truncate the decimal part.

    Unless you're supposed to enter cents instead of dollars?

    Anyway, it sounds like you might want to get some more clarification on your requirements.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    No, the requirements state clearly the output is to present both "dollar and cents" lead by a dollar sign. I just feel stuck, if I ultimately cut off the decimal portion the numbers are wrong. I feel stupid and first time programming shouldn't cause this much of a run around.

  14. #14
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    Unless...I store the double as an int and then use Math.round to round to the nearest integer. I think?

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    You could, but then what's the point of reading in the decimals at all? It really sounds like you're confused about the requirements.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    aalvarez is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with JOptionPane

    I seem to have figured out that dilemma only to stumped by another. I cannot enter fractions either. Should they be converted into decimals, although if a user other than myself was typing in information they should not be forced to convert numbers.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with JOptionPane

    Sounds like you need to read in the input as a String and then parse it how you like.

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Help with JOptionPane

    Quote Originally Posted by aalvarez View Post
    I seem to have figured out that dilemma only to stumped by another. I cannot enter fractions either. Should they be converted into decimals, although if a user other than myself was typing in information they should not be forced to convert numbers.
    How did you figure out your first dilemma?

    Java has no magic understanding of fractions. You'll have to parse them and do the division yourself. BigDecimal might be of some use, although it's probably overkill for your needs.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JOptionPane help....
    By outforlunch in forum New To Java
    Replies: 1
    Last Post: 09-15-2012, 10:00 PM
  2. JOptionPane
    By hfbroady in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2010, 12:07 PM
  3. JOptionPane
    By izzy in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 05:55 PM
  4. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 11:20 AM
  5. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 10:40 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
  •