Results 1 to 6 of 6
Thread: Number format Exception
- 07-16-2012, 03:55 AM #1
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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 04:47 AM. Reason: Solved
- 07-16-2012, 04:06 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Number format Exception
It sounds like parseDouble() does not like the string you passed it.Java Code:double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
You can check with a line of code:
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.Java Code:System.out.println("About to parse string=\"" + _jlbInterest.getText().trim() + "\""); double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
- 07-16-2012, 04:25 AM #3
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
Re: Number format Exception
Thank you for the reponse here is the error
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Annual Interest Rate"Java Code:
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 04:31 AM.
- 07-16-2012, 04:28 AM #4
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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 04:33 AM.
- 07-16-2012, 04:47 AM #5
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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.
should have beenJava Code:double annualInt = Double.parseDouble(_jlbInterest.getText().trim());
Java Code:double annualInt = Double.parseDouble(_jtfInterest.getText().trim())
Last edited by aortell24; 07-16-2012 at 04:51 AM.
- 07-16-2012, 05:22 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 22
- Rep Power
- 0
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
-
Number format
By asai in forum New To JavaReplies: 12Last Post: 06-21-2012, 10:24 AM -
Number Format Exception while parsing long
By Aamir in forum NetworkingReplies: 2Last Post: 05-15-2011, 02:57 AM -
catch the number format exception, where to place try
By aborgeld in forum New To JavaReplies: 7Last Post: 12-30-2010, 01:42 PM -
How to change number format of a field programmatically in Jasper?
By newbiejava in forum New To JavaReplies: 16Last Post: 09-04-2010, 04:03 AM -
Date format exception
By chaudhas in forum New To JavaReplies: 7Last Post: 06-25-2010, 09:31 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks