我如何让我的程序从一个输入字段计算两个方程式?

How would I have my program calculate two equations from one input field?

我需要从用户那里计算出两个函数,并且想知道完成后如何使用相同的输入字段在对话框中显示两个答案。我在 comp sci class 的第四周,我觉得我遇到了一些麻烦。我们被要求使用 JOptionPane 来执行此程序,但我想做更多并创建了我自己的程序。

到目前为止我所做的是,该程序已在 JGrasp 和 Eclipse 中正确编译,但在我 运行 应用程序时显示了这一点。

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 Nicolas_Carabajal_Assignment3$CalcButtonListener.actionPerformed(Nicolas_Carabajal_Assignment3.java:57)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    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:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:702)
    at java.awt.EventQueue.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue.run(EventQueue.java:724)
    at java.awt.EventQueue.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    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)

代码:

 /**
 * 
 *@author ngc5043
 *@version 1.0 
 */

import javax.swing.*;
import java.awt.event.*;

public class Nicolas_Carabajal_Assignment3 extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField ExTextField;
private JButton calcButton;
private final int WINDOW_WIDTH = 310;
private final int WINDOW_HEIGHT = 100;


 public static void main(String[] args)
  {
      Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();
      main.buildPanel();
  }
  public Nicolas_Carabajal_Assignment3()
  {
    setTitle("Expressions Window");
    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
  }

   private void buildPanel()
   {
       messageLabel = new JLabel("Please Enter a Number");
       ExTextField = new JTextField(10);
       calcButton = new JButton("Calculate");
       calcButton.addActionListener(new CalcButtonListener());
       panel = new JPanel();
       panel.add(messageLabel);
       panel.add(ExTextField);
       panel.add(calcButton);
   }

   private class CalcButtonListener implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
       {
           String input;
           int num_ent = 0;
           double answerOne;


           input = ExTextField.getText();
           answerOne = Double.parseDouble(input);
           answerOne = num_ent * num_ent;
           JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);

        /**
        * here i would like to add another function. input*(n+1.0)/2.0
        *Getting the same number from the user and calculating this aswell. 
        */ 



       }

   }


}

这一行: answerOne = Double.parseDouble(输入); 正在抛出异常,因为输入为空。

如果您在此处查看: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html 搜索 parseDouble ,您将在文档中找到以下内容: NumberFormatException - 如果字符串不包含可解析的双精度数。

如果您检查堆栈跟踪,您会看到: 在 java.lang.Double.parseDouble(Double.java:538)

这就是你知道问题出在哪里的方式。

我的建议是使用NetBeans或Eclipse之类的东西来帮助你在代码抛出异常时定位行号并逐行调试它以查看变量内容的变化。

在行周围使用Try/Catch:


input = ExTextField.getText();

或者检查输入,比如 isNumber(input)

这样你就可以避免这种意想不到的错误。

希望对您有所帮助! ;)

考虑以下代码。它做了两件事:如果输入不是数字,则捕获 NumberFormatException,并添加一个 answerTwo 变量,该变量被计算并连接到答案显示字符串。

private class CalcButtonListener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
    String input = ExTextField.getText();
    try  {
      double num_ent = Double.parseDouble(input);
      double answerOne = num_ent * num_ent;
      double answerTwo = num_ent * (num_ent + 1.0) / 2.0;
      JOptionPane.showMessageDialog(null, "Your Answers are " + answerOne + " and " + answerTwo);
    } catch (NumberFormatException e) {
      JOptionPane.showMessageDialog(null, "Invalid input " + input);
    }
  }
}

您两次调用了 buildpanel 方法,这就是字符串为空的原因,现在它将字符串转换为双精度并打印出来:

     /**
 * 
 *@author ngc5043
 *@version 1.0 
 */

import javax.swing.*;
import java.awt.event.*;

public class Nicolas_Carabajal_Assignment3 extends JFrame
{
    private JPanel panel;
    private JLabel messageLabel;
    private JTextField ExTextField;
    private JButton calcButton;
    private final int WINDOW_WIDTH = 310;
    private final int WINDOW_HEIGHT = 100;


 public static void main(String[] args)
  {
      Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();

  }
  public Nicolas_Carabajal_Assignment3()
  {
      buildPanel();
    setTitle("Expressions Window");
    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(panel);
    setVisible(true);
  }

   private void buildPanel()
   {
       messageLabel = new JLabel("Please Enter a Number");
       ExTextField = new JTextField(10);
       calcButton = new JButton("Calculate");

       panel = new JPanel();
       panel.add(messageLabel);
       panel.add(ExTextField);
       panel.add(calcButton);
       calcButton.addActionListener(new CalcButtonListener());
   }

   private class CalcButtonListener implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
       {
           String input;
           int num_ent = 0;


           input = ExTextField.getText();

           System.out.println(input);

           double answerOne = Double.parseDouble(input);
           System.out.println(answerOne);
           answerOne = num_ent * num_ent;
           JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);

        /**
        * here i would like to add another function. input*(n+1.0)/2.0
        *Getting the same number from the user and calculating this aswell. 
        */ 



       }

   }


}

异常堆栈显示 java.lang.NumberFormatException: empty String 继承自 java.lang.Double.parseDouble 方法。 Java 文档 (public static double parseDouble(String s) throws NumberFormatException) 写道 NumberFormatException 被抛出 "if the string does not contain a parsable double".

因此,问题是调用了按钮的操作处理程序,但文本框不包含任何可解析的值。要解决此问题,您必须处理抛出的异常。可能的解决方案如下:

public void actionPerformed(ActionEvent e)
{
    String input;
    int num_ent = 0;
    double answerOne;

    input = ExTextField.getText();
    try
    {
        answerOne = Double.parseDouble(input);
    }
    catch (NumberFormatException ex)
    { 
       JOptionPane.showMessageDialog(null, "Bad or empty input field");
       return;
    }
    //...
}