Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By aortell24

Thread: Number format Exception

  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Number format Exception

    [SOLVED]Hello everyone I am getting a number format exception pointing to line 81 which on mine is the double annual Interest rate definition.I have looked through my book and I cant figuer out what I did wrong could some please take a look at this for me Thanks.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/15/12
     * Time: 6:36 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise34_01 extends JFrame{
        private JTextField _jtfAmount = new JTextField();
        private JTextField _jtfYears = new JTextField();
        private JTextField _jtfInterest = new JTextField();
        private JTextField _jtfValue = new JTextField();
        private JLabel _jlbAmount = new JLabel(" Interest Amount");
        private JLabel _jlbYears = new JLabel(" Years");
        private JLabel _jlbInterest = new JLabel(" Annual Interest Rate   ");
        private JLabel _jlbValue = new JLabel(" Future Value");
        private JPanel _jlbPanel = new JPanel();
        private JPanel _txtPanel = new JPanel();
        private JButton _jbtCalc = new JButton(" Calculate");
        private BoxLayout _txtPanelLayout;
        private BoxLayout _jlbPanelLayout;
        private JMenuBar _menuBar = new JMenuBar();
        private JMenu _opMenu = new JMenu("Operation");
        private JMenu _helpMenu = new JMenu("Help");
        
        public Exercise34_01()
        {
             //Layout
             this.setLayout(new BorderLayout());
            _txtPanelLayout = new BoxLayout(_txtPanel,BoxLayout.Y_AXIS);
            _jlbPanelLayout = new BoxLayout(_jlbPanel,BoxLayout.Y_AXIS);
            _txtPanel.setLayout(_txtPanelLayout);
            _jlbPanel.setLayout(_jlbPanelLayout);
            Box jlbBox = Box.createVerticalBox();
            
            //textFields
            _jtfAmount.setPreferredSize(new Dimension(100,25));
            _jtfYears.setPreferredSize(new Dimension(100,25));
            _jtfInterest.setPreferredSize(new Dimension(100,25));
            _jtfValue.setPreferredSize(new Dimension(100,25));
    
            //_txtPanel
            _txtPanel.add(_jtfAmount);
            _txtPanel.add(_jtfYears);
            _txtPanel.add(_jtfInterest);
            _txtPanel.add(_jtfValue);
            this.add(_txtPanel, BorderLayout.EAST);
            
            //JlbPanel
            jlbBox.add(_jlbAmount);
            jlbBox.add(Box.createVerticalStrut(8));
            jlbBox.add(_jlbYears);
            jlbBox.add(Box.createVerticalStrut(9));
            jlbBox.add(_jlbInterest);
            jlbBox.add(Box.createVerticalStrut(9));
            jlbBox.add(_jlbValue);
           
            this.add(jlbBox, BorderLayout.WEST);
            this.add(_jbtCalc,BorderLayout.SOUTH);
    
            //Actions
            Action exitAction = new AbstractAction("Exit")
            {
                @Override
                public void actionPerformed(ActionEvent actionEvent)
                {
                    System.exit(0);
                }
            };
            Action calcAction = new AbstractAction("Calculate")
            {
                @Override
                public void actionPerformed(ActionEvent actionEvent)
                {
                    double investmentAmount = Double.parseDouble(_jtfAmount.getText().trim());
                    double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
                    System.out.print(annualInt);
                    int years = Integer.parseInt(_jtfYears.getText().trim());
                    double futureVal = investmentAmount * (1 + annualInt)* years*12;
                     _jtfValue.setText(String.valueOf(futureVal));
                }
            };
    
            //Menu
            this.setJMenuBar(_menuBar);
            _menuBar.add(_opMenu);
            _menuBar.add(_helpMenu);
    
            _helpMenu.add(new JMenuItem("About"));
             _opMenu.add(exitAction);
            _opMenu.add(calcAction);
            
        }  
      
    }
    import javax.swing.*;
    
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/15/12
     * Time: 3:52 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Application {
        public static void main(String[] args)
        {
            Exercise34_01 frame = new Exercise34_01();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            frame.pack();
        }
    }
    Last edited by aortell24; 07-16-2012 at 05:47 AM. Reason: Solved

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Number format Exception

    Java Code:
    double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
    It sounds like parseDouble() does not like the string you passed it.

    You can check with a line of code:

    Java Code:
    System.out.println("About to parse string=\"" + _jlbInterest.getText().trim() + "\"");
    double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
    If you do not understand why the string you see is bad, post the output. And if the string is not what you were expecting you will have to figure out why getText() returned the value it did.

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Number format Exception

    Thank you for the reponse here is the error
    Java Code:
      
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Annual Interest Rate"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1242)
    at java.lang.Double.parseDouble(Double.java:527)
    at Exercise34_01$2.actionPerformed(Exercise34_01.java :82)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton. java:374)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:829)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:873)
    at java.awt.Component.processMouseEvent(Component.jav a:6268)
    About to parse string="Annual Interest Rate"
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 3)
    at java.awt.Container.processEvent(Container.java:204 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4629)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2103)
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517 )
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)
    Last edited by aortell24; 07-16-2012 at 05:31 AM.

  4. #4
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Number format Exception

    I passed 10000 to the investment amount 2 to the years and 5.75 to the interest Acording to my calculations this should return 11,215.65.I dont understand why the Annual Interest Rate is being mentioned its only the text displayed in that label.
    Last edited by aortell24; 07-16-2012 at 05:33 AM.

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Number format Exception

    I figured it out I made a big error I was getting the text from the label instead of the textfield I am so sorry for wasting your time but thank you so much for your help.
    Java Code:
    double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
    should have been
    Java Code:
    double annualInt = Double.parseDouble(_jtfInterest.getText().trim())
    Last edited by aortell24; 07-16-2012 at 05:51 AM.
    jfabian likes this.

  6. #6
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default Re: Number format Exception

    I'm happy you find the mistake, aortell24. I want you take into considaration don't name reference variable with special character. Maybe the IDE generate that variables name, but in that case try to change the name, this just will help others programmers to quickly understands your code, remenber the Java Nomenclature. Anyways, keep it going! this is just and advice!

Similar Threads

  1. Number format
    By asai in forum New To Java
    Replies: 12
    Last Post: 06-21-2012, 11:24 AM
  2. Number Format Exception while parsing long
    By Aamir in forum Networking
    Replies: 2
    Last Post: 05-15-2011, 03:57 AM
  3. Replies: 7
    Last Post: 12-30-2010, 02:42 PM
  4. Replies: 16
    Last Post: 09-04-2010, 05:03 AM
  5. Date format exception
    By chaudhas in forum New To Java
    Replies: 7
    Last Post: 06-25-2010, 10:31 AM

Posting Permissions

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