为什么这段代码总是进入catch块?

时间:2016-06-18 05:50:16

标签: java exception

我正在创建一个简单的计算器。每当我尝试执行任何操作时,它都会进入catch块。这是代码。 textFields如下: textfield(显示计算),textField1(输入操作数),textfield_1(显示解决方案)

button = new JButton("+");
    button.setForeground(Color.BLACK);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                ans=Double.parseDouble(textField1.getText());
                s=Double.toString(ans)+"+";
                textField.setText(s);
                reset();
                operand=Double.parseDouble(textField1.getText());
                ans+=operand;
                textField_1.setText(Double.toString(ans));
            }catch(Exception x){
                JOptionPane.showMessageDialog(null, "Please Enter a valid argument");

            }

        }
    });
    button.setBounds(43, 115, 89, 23);
    frame.getContentPane().add(button);
    button.setToolTipText("ADDITION");
//reset textField1  
public void reset(){
    textField1.setText("");
}

//............//

java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at gui.Calcu$2.actionPerformed(Calcu.java:92)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:3)

你的问题在这里:

reset();//this makes the textField1 hold an empty String 
operand=Double.parseDouble(textField1.getText());

因此,当您尝试从中读取并解析Double时,您会发现NumberFormatException,因为您无法将空String转换为Double。

你的问题的一个解决方案是创建第二个JTextField并让用户将他想要添加的第一个数字写入第一个textField,将第二个数字写入第二个。然后,当他按下按钮查看结果时每个数字分别处理它们并将结果显示在屏幕上(在两个textFileds中的一个上,或者你可以使用第三个来显示结果)。

另一方面,如果你想要一个textField来处理用户输入,你可以在所有方法之外声明一个变量sum(使其成为全局),每次按下" +& #34;按钮将值添加到sum中,当用户完成输入数字时,只需将其String值显示在屏幕上。

有关event-driven编程如何运作的其他信息。

答案 1 :(得分:0)

在按钮点击列表器中尝试以下代码,

  button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try{
              final double val1 = Double.parseDouble(textField1.getText());
              ans = val1;
              s = Double.toString(ans) + "+";
              textField.setText(s);
              reset();
              operand = val1;
              ans += operand;
              textField_1.setText(Double.toString(ans));
          }catch(Exception x){
              JOptionPane.showMessageDialog(null, "Please Enter a valid argument");

          }

      }
  });