1

I am making a travel cost calculator for my class in Java using TextPad. The program is supposed to take input from the user via text field on a GUI and then make the following calculation: (miles*gaspergallon)+oilchange. I am able to compile the code but I get the following error:

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

Here is my code:

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);
    }
}
1
  • 1
    you should enter values in respective text fields. Commented Nov 17, 2015 at 4:32

1 Answer 1

0

It will be, because you just take value from text field and parse to Double without checking.

At the first time, when program is start, it execute total. But every field are blank. Blank cannot parse to double. So, it shown error.

So, I suggested to use like this:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.