4 Replies - 415 Views - Last Post: 06 October 2013 - 03:25 PM Rate Topic: -----

#1 aalvarez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Parsing Strings- Using Integer.parseInt and Entering Double

Posted 06 October 2013 - 03:18 PM

I created the dialog boxes but as I attempt to put in the amount (specifically the dollar amount with the decimal I get an error. This isn't for a grade but our exams are based off of the errors we correct, how, etc.

Error message: Exception in thread "main" java.lang.NumberFormatException: For input string: "3762.50"

public class StockTester {

    /**
     * @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);      

This post has been edited by macosxnerd101: 06 October 2013 - 03:19 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Parsing Strings- Using Integer.parseInt and Entering Double

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Parsing Strings- Using Integer.parseInt and Entering Double

Posted 06 October 2013 - 03:20 PM

Integer.parseInt() expects the String to be a valid integer. Is 3762.50 an integer? No. You will want to use doubles and Double.parseDouble() instead.
Was This Post Helpful? 1
  • +
  • -

#3 aalvarez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: Parsing Strings- Using Integer.parseInt and Entering Double

Posted 06 October 2013 - 03:22 PM

Correct, but then the professor asks that in order to receive full credit all instance variables must be type int.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Parsing Strings- Using Integer.parseInt and Entering Double

Posted 06 October 2013 - 03:23 PM

You're not using any instance variables though. You could always just convert the prices to pennies and go from there.
Was This Post Helpful? 1
  • +
  • -

#5 aalvarez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: Parsing Strings- Using Integer.parseInt and Entering Double

Posted 06 October 2013 - 03:25 PM

Ah. You're right. Thank you, I didn't even think of doing that :stupid:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1