NumberFormatException-如何解决?

时间:2018-11-02 09:08:43

标签: java swing jtextfield numberformatexception

在输入jTextField2之前输入jTextField1时,我得到以下错误堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Enter"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at suvatcalc.CalculatorFrame.jTextField2KeyReleased(CalculatorFrame.java:263)
at suvatcalc.CalculatorFrame.access$200(CalculatorFrame.java:14)
at suvatcalc.CalculatorFrame$3.keyReleased(CalculatorFrame.java:78)
at java.awt.Component.processKeyEvent(Component.java:6500)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
at java.awt.Component.processEvent(Component.java:6316)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:835)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1103)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:974)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:800)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

代码如下:

private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {                                     
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        String displacementString = jTextField1.getText();
        displacement = Double.parseDouble(displacementString);
        System.out.println(displacement);
    }
}                                       

private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {                                        
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        String initialVelocityString = jTextField1.getText();
        initialVelocity = Double.parseDouble(initialVelocityString);
        System.out.println(initialVelocity);
    }

}                 

我不太清楚问题出在哪里。该程序要求我仅按我认为是编写程序方式的结果输入文本框。

1 个答案:

答案 0 :(得分:0)

在方法jTextField2KeyReleased()中,您尝试将非数字值解析为Double对象-> initialVelocity = Double.parseDouble(initialVelocityString);

您可以通过使用正则表达式来使用适当的验证,也可以捕获NumberFormatException并要求用户提供适当的输入。