Java计算器“相等”按钮不起作用

时间:2015-10-18 04:29:41

标签: java error-handling exception-handling calculator formatter

所以我正在研究Java中的计算器,除了我实际点击等号时,一切似乎都在工作。我得到了一个我以前从未见过的错误,我不确定它的含义。

我不确定我应该在这里发布多少代码,但由于唯一的突出显示/警告包含在相同的按钮操作事件中,我将首先发布该代码。

 private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          
        secondNum = Double.parseDouble(calcField.getText());
        if (operation == "+") {
            result = firstNum + secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);
        } else  if (operation == "-") {
            result = firstNum - secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        } else if (operation == "*") {
            result = firstNum * secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        } else if(operation == "/") {
            result = firstNum / secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        }
    }                                         

这是我的屏幕图片:

https://i.gyazo.com/8b15ec8b1c7ec7982122230075b8370a.png

以下是我收到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at online.billing.system.BillingGUI.btnEqualsActionPerformed(BillingGUI.java:351)
    at online.billing.system.BillingGUI.access$1600(BillingGUI.java:8)
    at online.billing.system.BillingGUI$17.actionPerformed(BillingGUI.java:172)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    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:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at online.billing.system.BillingGUI.btnEqualsActionPerformed(BillingGUI.java:351)
    at online.billing.system.BillingGUI.access$1600(BillingGUI.java:8)
    at online.billing.system.BillingGUI$17.actionPerformed(BillingGUI.java:172)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    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:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    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)
BUILD SUCCESSFUL (total time: 4 seconds)

如果有人愿意为我提供一些关于我做错事的见解,我将非常感激。感谢您阅读我的帖子。

2 个答案:

答案 0 :(得分:0)

我认为从这一行抛出了异常:

secondNum = Double.parseDouble(calcField.getText());

例外是:

java.lang.NumberFormatException: empty String

这个例外说,对于解析,传递了一个空字符串。 确保calcField.Text不为空。 希望这会有所帮助。

答案 1 :(得分:-1)

基本上它说你在尝试解析为双倍时有一个空字符串,所以你应该在之前进行检查:

if(calcField.getText() != null && !calcField.getText().isEmpty())

您的代码看起来像这样(我自己没试过):

private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          
    if(calcField.getText() != null && !calcField.getText().isEmpty()) {
        secondNum = Double.parseDouble(calcField.getText());
        if (operation == "+") {
            result = firstNum + secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);
        } else  if (operation == "-") {
            result = firstNum - secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        } else if (operation == "*") {
            result = firstNum * secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        } else if(operation == "/") {
            result = firstNum / secondNum;
            answer = String.format("%.of", result);
            calcField.setText(answer);  
        }
    }          
}