Java.lang.NumberFormatException错误: empty string- Java Application

标签 java swing

我正在使用 TextPad 为我的类(class)使用 Java 制作旅行费用计算器。该程序应该通过 GUI 上的文本字段获取用户的输入,然后进行以下计算:(miles*gaspergallon)+oilchange。我能够编译代码,但出现以下错误:

Exception in thread "main" java.lang.numberformatexception: empty string.

这是我的代码:

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

public class GasCalc extends JFrame {
    JTextField jtfMiles, jtfCostPerGallon, jtfOilChangeCost, jtfInfo1, jtfInfo2, jtfInfo3;
    Double total = 0.00;
    Double miles = 0.00;
    Double costpergallon = 0.00;
    Double oilchange = 0.00;
    String display = "";
    TextHandler handler = null;
    public GasCalc(){
        super("Travelor's Gasoline Calculator");
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        jtfInfo1 = new JTextField("Please Enter the number of miles you will travel.",40);
        jtfInfo1.setEditable(false);
        jtfMiles = new JTextField(10);
        jtfInfo2 = new JTextField("Please Enter the cost per gallon.",30);
        jtfInfo2.setEditable(false);
        jtfCostPerGallon = new JTextField(10);
        jtfInfo3 = new JTextField("Please Enter the cost of oil change, if applicable.",45);
        jtfInfo3.setEditable(false);
        jtfOilChangeCost = new JTextField("0.00",10);

        container.add(jtfInfo1);
        container.add(jtfMiles);
        container.add(jtfInfo2);
        container.add(jtfCostPerGallon);
        container.add(jtfInfo3);
        container.add(jtfOilChangeCost);

        handler = new TextHandler();

        jtfInfo1.addActionListener(handler);
        jtfMiles.addActionListener(handler);
        jtfInfo2.addActionListener(handler);
        jtfCostPerGallon.addActionListener(handler);
        jtfInfo3.addActionListener(handler);
        jtfOilChangeCost.addActionListener(handler);

        miles = Double.parseDouble(jtfMiles.getText());
        costpergallon = Double.parseDouble(jtfCostPerGallon.getText());
        oilchange = Double.parseDouble(jtfOilChangeCost.getText());

        total = (miles*costpergallon)+ oilchange;

        setSize(500,500);
        setVisible(true);
    }
    private class TextHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == total){
                display = "Your total is: " + e.getActionCommand();
            } else if (e.getSource() == jtfInfo1){
                display = "Your total is not: " + e.getActionCommand();
            }
            JOptionPane.showMessageDialog(null,display);
        }
    }
    public static void main(String args[]){
        GasCalc test = new GasCalc();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

最佳答案

会的,因为您只需从文本字段中获取值并解析为 Double 而不进行检查。

程序第一次启动时,会执行total。但每个字段都是空白的。 Blank 无法解析为 double。所以,它显示错误。

所以,我建议这样使用:

if(jtfMiles.getText().isEmpty()){
    JOptionPane.showMessageDialog("Error Found in execution!!!","Miles field should not empty.");
} else if (jtfCostPerGallon.getText().isEmpty()){
    JOptionPane.showMessageDialog("Error Found in execution!!!","Cost per Gallon field should not empty.");
} else if (jtfOilChangeCost.getText().isEmpty()){
    JOptionPane.showMessageDialog("Error Found in execution!!!","Oil change cost field should not empty.");
} else {
    miles = Double.parseDouble(jtfMiles.getText());
    costpergallon = Double.parseDouble(jtfCostPerGallon.getText());
    oilchange = Double.parseDouble(jtfOilChangeCost.getText());

    total = (miles*costpergallon)+ oilchange;
}

关于Java.lang.NumberFormatException错误: empty string- Java Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749166/

相关文章:

java - 使用 crawler4j 发送请求中的 cookie?

java - 在 Mac 上设置 Java Swing 应用程序名称

java - 如何自定义 JFileChooser

java - 如何使 Jbutton 可点击并显示文本

java - 不幸的是,APP已停止 - Android Twitter客户端

java - PMD 用于检测未使用的代码 - API 使用情况

java - Mobilefirst 平台与 NETIQ IDAM 的集成

java - 动态 JComboBox 大小

java - Q关于Java WindowListener

java - 如何在数据库中只存储图像路径(URL)而不是图像本身?