Java 计算器 "equal" 按钮不起作用

Java Calculator "equal" button not working

所以我正在 Java 中使用计算器,除了我实际单击等号时,一切似乎都正常。我收到一个我以前从未见过的错误,我不确定它是什么意思。

我不确定我应该在此处 post 编写多少代码,但由于只有 highlights/warning 包含在相等按钮操作事件中,所以我将首先 post 该代码。

 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.access00(BillingGUI.java:8)
    at online.billing.system.BillingGUI.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.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.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.run(EventQueue.java:731)
    at java.awt.EventQueue.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.access00(BillingGUI.java:8)
    at online.billing.system.BillingGUI.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.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.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.run(EventQueue.java:731)
    at java.awt.EventQueue.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)

如果有人愿意就我做错了什么向我提供一些见解,我将不胜感激。感谢阅读我的帖子。

我假设从这一行抛出异常:

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

例外情况是:

java.lang.NumberFormatException: empty String

并且此异常表明,为了进行解析,传递了一个空字符串。 确保 calcField.Text 不为空。 希望这有帮助。

基本上是说您在尝试解析为 double 时有一个空字符串,因此您应该在之前进行此检查:

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);  
        }
    }          
}