簡體   English   中英

Java.lang.NumberFormatException錯誤:空字符串-Java應用程序

[英]Java.lang.NumberFormatException error: empty string- Java Application

我正在使用TextPad在Java中為我的課程制作旅行費用計算器。 該程序應該通過GUI上的文本字段從用戶那里獲取輸入,然后進行以下計算: (miles*gaspergallon)+oilchange 我可以編譯代碼,但是出現以下錯誤:

線程“主”中的異常java.lang.numberformatException:空字符串。

這是我的代碼:

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。

第一次啟動程序時,將執行總計。 但是每個字段都是空白。 空白無法解析為兩倍。 因此,它顯示了錯誤。

所以,我建議這樣使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM