I have to create a program that will calculate the the average miles per tank and the cost to fill tank of a car. The inputs are tank capacity, miles per gallon, and price per gallon, the outputs are average miles per tank and cost to fill tank. I have to use the methods: getTankCapacity, getMpgRating, getGallonPrice, calculateAverageMiles, calculateFillCost, and displayOutput, and the inputs must also use try-catch. When I test the program leaving one of the text fields blank, the error message pops up multiple times and does not leave the output fields blank. I am unsure of how to fix this. Any help would be much appreciated!

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

    public class AverageMilesPerTank extends JFrame
    {

        Color black = new Color(0, 0, 0);
        Color white = new Color(255, 255, 255);
        Color light_gray = new Color(192, 192, 192);

        JLabel tankCapacityJLabel;
        JTextField tankCapacityJTextField;

        JLabel mpgRatingJLabel;
        JTextField mpgRatingJTextField;

        JLabel gallonPriceJLabel;
        JTextField gallonPriceJTextField;

        JLabel averageMilesJLabel;
        JTextField averageMilesJTextField;

        JLabel fillCostJLabel;
        JTextField fillCostJTextField;

        JButton enterJButton;
        JButton clearJButton;
        JButton closeJButton;

        DecimalFormat currency;

        int tankCapacity;
        double mpgRating;
        double gallonPrice;
        double averageMiles;
        double fillCost;

          public AverageMilesPerTank()
        {
                createUserInterface();
        }

        public void createUserInterface()
        {
            Container contentPane = getContentPane();
            contentPane.setBackground(Color.white);
            contentPane.setLayout(null);

            tankCapacityJLabel = new JLabel();
            tankCapacityJLabel.setBounds(50, 50, 120, 20);
            tankCapacityJLabel.setFont(new Font("Default", Font.PLAIN, 12));
            tankCapacityJLabel.setText("Tank Capacity");
            tankCapacityJLabel.setForeground(black);
            tankCapacityJLabel.setHorizontalAlignment(JLabel.LEFT);
            contentPane.add(tankCapacityJLabel);

            tankCapacityJTextField = new JTextField();
            tankCapacityJTextField.setBounds(225, 50, 75, 20);
            tankCapacityJTextField.setFont(new Font("Default", Font.PLAIN, 12));
            tankCapacityJTextField.setHorizontalAlignment(JTextField.CENTER);
            tankCapacityJTextField.setForeground(black);
            tankCapacityJTextField.setBackground(white);
            tankCapacityJTextField.setEditable(true);
            contentPane.add(tankCapacityJTextField);

            mpgRatingJLabel = new JLabel();
            mpgRatingJLabel.setBounds(50, 80, 150, 20);
            mpgRatingJLabel.setFont(new Font("Default", Font.PLAIN, 12));
            mpgRatingJLabel.setText("Miles Per Gallon Rating");
            mpgRatingJLabel.setForeground(black);
            mpgRatingJLabel.setHorizontalAlignment(JLabel.LEFT);
            contentPane.add(mpgRatingJLabel);

            mpgRatingJTextField = new JTextField();
            mpgRatingJTextField.setBounds(225, 80, 75, 20);
            mpgRatingJTextField.setFont(new Font("Default", Font.PLAIN, 12));
            mpgRatingJTextField.setHorizontalAlignment(JTextField.CENTER);
            mpgRatingJTextField.setForeground(black);
            mpgRatingJTextField.setBackground(white);
            mpgRatingJTextField.setEditable(true);
            contentPane.add(mpgRatingJTextField);

            gallonPriceJLabel = new JLabel();
            gallonPriceJLabel.setBounds(50, 110, 150, 20);
            gallonPriceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
            gallonPriceJLabel.setText("Current Price Per Gallon");
            gallonPriceJLabel.setForeground(black);
            gallonPriceJLabel.setHorizontalAlignment(JLabel.LEFT);
            contentPane.add(gallonPriceJLabel);

            gallonPriceJTextField = new JTextField();
            gallonPriceJTextField.setBounds(225, 110, 75, 20);
            gallonPriceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
            gallonPriceJTextField.setHorizontalAlignment(JTextField.CENTER);
            gallonPriceJTextField.setForeground(black);
            gallonPriceJTextField.setBackground(white);
            gallonPriceJTextField.setEditable(true);
            contentPane.add(gallonPriceJTextField);

            averageMilesJLabel = new JLabel();
            averageMilesJLabel.setBounds(50, 170, 150, 20);
            averageMilesJLabel.setFont(new Font("Default", Font.PLAIN, 12));
            averageMilesJLabel.setText("Average Miles Per Tank");
            averageMilesJLabel.setForeground(black);
            averageMilesJLabel.setHorizontalAlignment(JLabel.LEFT);
            contentPane.add(averageMilesJLabel);

            averageMilesJTextField = new JTextField();
            averageMilesJTextField.setBounds(225, 170, 75, 20);
            averageMilesJTextField.setFont(new Font("Default", Font.PLAIN, 12));
            averageMilesJTextField.setHorizontalAlignment(JTextField.CENTER);
            averageMilesJTextField.setForeground(black);
            averageMilesJTextField.setBackground(white);
            averageMilesJTextField.setEditable(false);
            contentPane.add(averageMilesJTextField);

            fillCostJLabel = new JLabel();
            fillCostJLabel.setBounds(50, 200, 150, 20);
            fillCostJLabel.setFont(new Font("Default", Font.PLAIN, 12));
            fillCostJLabel.setText("Cost to Fill Tank");
            fillCostJLabel.setForeground(black);
            fillCostJLabel.setHorizontalAlignment(JLabel.LEFT);
            contentPane.add(fillCostJLabel);

            fillCostJTextField = new JTextField();
            fillCostJTextField.setBounds(225, 200, 75, 20);
            fillCostJTextField.setFont(new Font("Default", Font.PLAIN, 12));
            fillCostJTextField.setHorizontalAlignment(JTextField.CENTER);
            fillCostJTextField.setForeground(black);
            fillCostJTextField.setBackground(white);
            fillCostJTextField.setEditable(false);
            contentPane.add(fillCostJTextField);

            enterJButton = new JButton();
            enterJButton.setBounds(25, 300, 100, 20);
            enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
            enterJButton.setText("Enter");
            enterJButton.setForeground(black);
            enterJButton.setBackground(white);
            contentPane.add(enterJButton);
            enterJButton.addActionListener(

             new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        enterJButtonActionPerformed(event);
                    }
                }
            );

            clearJButton = new JButton();
            clearJButton.setBounds(150, 300, 100, 20);
            clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
            clearJButton.setText("Clear");
            clearJButton.setForeground(black);
            clearJButton.setBackground(white);
            contentPane.add(clearJButton);
            clearJButton.addActionListener(

                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        clearJButtonActionPerformed(event);
                    }
                }
            );

            closeJButton = new JButton();
            closeJButton.setBounds(275, 300, 100, 20);
            closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
            closeJButton.setText("Close");
            closeJButton.setForeground(black);
            closeJButton.setBackground(white);
            contentPane.add(closeJButton);
            closeJButton.addActionListener(

                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        closeJButtonActionPerformed(event);
                    }
                }
            );

          setTitle("Fuel Cost");
            setSize(400, 400);
            setVisible(true);
        }


        public static void main(String[] args)
        {
                AverageMilesPerTank application = new AverageMilesPerTank();
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public void enterJButtonActionPerformed(ActionEvent event)
        {
          //must list all methods here
          tankCapacity = getTankCapacityInput();
          mpgRating = getMpgRatingInput();
          gallonPrice = getGallonPriceInput();
          averageMiles = calculateAverageMiles();
          fillCost = calculateFillCost();
          displayOutput();
        }


        public int getTankCapacityInput()
        {
            try
            {
                tankCapacity = Integer.parseInt(tankCapacityJTextField.getText());
                getMpgRatingInput();
            }
            catch(NumberFormatException exception)
            {
                JOptionPane.showMessageDialog(this,
                "Please enter Tank Capacity!",
                "Number Format Error", JOptionPane.ERROR_MESSAGE );
                tankCapacityJTextField.setText("");
                tankCapacityJTextField.requestFocusInWindow();
            }
       return tankCapacity;
        }


        public double getMpgRatingInput()
        {
            try
            {
                mpgRating = Double.parseDouble(mpgRatingJTextField.getText());
                getGallonPriceInput();
            }
            catch(NumberFormatException exception)
            {
                JOptionPane.showMessageDialog(this,
                "Please enter Miles Per Gallon Rating!",
                "Number Format Error", JOptionPane.ERROR_MESSAGE );
                mpgRatingJTextField.setText("");
                mpgRatingJTextField.requestFocusInWindow();
            }
       return mpgRating;
        }

       public double getGallonPriceInput()
        {
            try
            {
                gallonPrice = Double.parseDouble(gallonPriceJTextField.getText());
                calculateFillCost();
            }
            catch(NumberFormatException exception)
            {
                JOptionPane.showMessageDialog(this,
                "Please enter Current Price Per Gallon!",
                "Number Format Error", JOptionPane.ERROR_MESSAGE );
                gallonPriceJTextField.setText("");
                gallonPriceJTextField.requestFocusInWindow();
            }
       return gallonPrice;
        }

       public double calculateAverageMiles()
       {
          averageMiles = mpgRating * tankCapacity;   

       return averageMiles;
       }

       public double calculateFillCost()
       {

          currency = new DecimalFormat("$0.00");
          fillCost = tankCapacity * gallonPrice;


       return fillCost; 
        }

       public void displayOutput()
       {
          averageMilesJTextField.setText("" + averageMiles);
          fillCostJTextField.setText("" + currency.format(fillCost));
       }


        public void clearJButtonActionPerformed(ActionEvent event)
        {
            tankCapacityJTextField.setText("");
            tankCapacityJTextField.requestFocusInWindow();
            mpgRatingJTextField.setText("");
            gallonPriceJTextField.setText("");
            averageMilesJTextField.setText("");
            fillCostJTextField.setText("");
        }
       public void closeJButtonActionPerformed(ActionEvent event)
       {
          System.exit(0);
       }



    }

Each of your get methods calls the next get method, despite the fact that the original actionPerformed calls them all anyway. This will result in multiple calls to the later get methods, and thus mutiple error messages

Comments
Thank you for your help! I deleted those lines calling the next method, and it no longer pops up the error message several times. However, it is still putting text in the output text fields when it should not. How can I fix this?

To resolve this, you must use the try-catch differently : your methods must throw exceptions not catch them, and the try-catch bloc will be in the enterJButtonActionPerformed method.

A simple tutorial here

This question has already been answered. Start a new discussion instead.