3 Replies - 172 Views - Last Post: 28 January 2018 - 11:30 AM Rate Topic: -----

#1 bman8932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 17

Try Catch Block Help

Posted 27 January 2018 - 07:21 PM

I can't get this try catch block to work. I want it to catch the error (like if the user enters a letter) then display a message telling them what they did wrong. Any help would be great. Thanks.
//This program calculates and displays the total travel expenses of a business person on a trip.

import javax.swing.*;
import java.awt.event.*;
public class TravelExpenses extends JFrame
{
    // required components and variables
    private JPanel panel;
    private JLabel ldays, lairFareInput, lcar, lmiles, lparking, ltaxi, llodge, meals, parking, taxi, lseminar, lodge, mile;
    private JTextField tdays, tairFareInput, tcar, tmiles, tparking, ttaxi, tseminar, tlodge;
    private JButton calculations;
    
    private final double MEALS_PER_DAY = 37.00;
    private final double PARKING_PER_DAY = 10.00;
    private final double TAXI_PER_DAY = 20.00;
    private final double LODGING_PER_DAY = 95.00;
    private final double D_PER_MILE = .27;
    
    private final int WINDOW_WIDTH = 280;
    private final int WINDOW_HEIGHT = 500;
    
    //constructor
    public TravelExpenses()
    {
    // set the title bar text 
        setTitle("Travel Expenses");
        
    // set the size of the window
    setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
    
    // decide what happens when the default close button is clicked
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // building the panel
    buildPanel();
    
    // add panel to the content pane
    add(panel);
    
    // display the window
    setVisible(true);
    } // end of consructor
    
    // buildPanel method
    private void buildPanel()
    {
    // create JPanel
        panel = new JPanel();
        
        // creating the labels
        ldays = new JLabel("Days on trip: ");
        lairFareInput = new JLabel ("Amount of airfare: $");
        lcar = new JLabel("Amount of car rental: $");
        lmiles = new JLabel ("Miles driven: $");
        lparking = new JLabel ("Parking fees: $");
        ltaxi = new JLabel ("Taxi fees: $");
        lseminar = new JLabel ("Seminar fee(s): $");
        llodge = new JLabel ("Lodging fee per night: $");
        
        meals = new JLabel ("\nMeals per day: $" + MEALS_PER_DAY);
        parking = new JLabel ("Parking fees per day: $" + PARKING_PER_DAY);
        taxi = new JLabel ("Taxi charges per day: $" + TAXI_PER_DAY);
        lodge = new JLabel ("Lodging charges per day: $" + LODGING_PER_DAY);
        mile = new JLabel ("Charges per mile: $" + D_PER_MILE);
        
        // create text fields
        tdays = new JTextField(10);
        tcar = new JTextField(10);
        tairFareInput = new JTextField(10);
        tmiles = new JTextField(10);
        tparking = new JTextField(10);
        ttaxi = new JTextField(10);
        tseminar = new JTextField(10);
        tlodge = new JTextField(10);
        
        // creating a button
        calculations = new JButton("Calculate Reimbursement");
        calculations.addActionListener(new Calc());
        
        // adding the components to the panel
        panel.add(ldays);
        panel.add(tdays);
        panel.add(lairFareInput);
        panel.add(tairFareInput);
        panel.add(lcar);
        panel.add(tcar);
        panel.add(lmiles);
        panel.add(tmiles);
        panel.add(lparking);
        panel.add(tparking);
        panel.add(ltaxi);
        panel.add(ttaxi);
        panel.add(lseminar);
        panel.add(tseminar);
        panel.add(llodge);
        panel.add(tlodge);
        
        panel.add(meals);
        panel.add(parking);
        panel.add(taxi);
        panel.add(lodge);
        panel.add(mile);
        panel.add(calculations);
    } // end buildPanel method
    
    // implementing an action listener via Calc class
    public class Calc implements ActionListener
    {
    // this method executes when the user clicks on the display the details button
        public void actionPerformed(ActionEvent buttonclicked)
        {
        double totalExpenses;
        double allowedExpenses;
        
        double days = Integer.parseInt(tdays.getText());
        double airFareInput = Double.parseDouble(tairFareInput.getText());
        double carRentalInput = Double.parseDouble(tcar.getText());
        double milesDrivenInput = Double.parseDouble(tmiles.getText());
        double parkingInput = Double.parseDouble(tparking.getText());
        double taxiInput = Double.parseDouble(ttaxi.getText());
        double seminarInput = Double.parseDouble(tseminar.getText());
        double lodgingInput = Double.parseDouble(tlodge.getText());
        
        try { // try block
        
        // calculating the total expenses
        totalExpenses = (MEALS_PER_DAY * days) + airFareInput + carRentalInput + (milesDrivenInput * D_PER_MILE) + parkingInput +
                taxiInput + seminarInput + (lodgingInput * days);

        // calculating expenses covered by company
        double taxiLimit = (TAXI_PER_DAY * days);
        double lodgingLimit = (LODGING_PER_DAY * days);
        double parkingLimit = (PARKING_PER_DAY * days);
        
        // taxi limit
        if (taxiInput > taxiLimit)
        {
        taxiInput = taxiLimit;
        }

        // lodging limit
        if (lodgingInput > lodgingLimit)
        {
        lodgingInput = lodgingLimit;
        }

        // parking limit
        if (parkingInput > parkingLimit)
        {
        parkingInput = parkingLimit;
        }
        
        allowedExpenses = (MEALS_PER_DAY * days) + airFareInput + carRentalInput + (milesDrivenInput * D_PER_MILE) + parkingInput +
                taxiInput + seminarInput + lodgingLimit;
        

        //determine whether or not total expenses are more then allowed expenses
        if(totalExpenses > allowedExpenses)
            JOptionPane.showMessageDialog(null, "Total expenses by business person: $" + Double.toString(totalExpenses) + 
                    "\n Allowable expenses for the trip: $" + Double.toString(allowedExpenses) + 
                    "\nExpenses must be paid by business person: $" + Double.toString((totalExpenses - allowedExpenses)));
        else
            JOptionPane.showMessageDialog(null, "Total expenses by business person: $" + 
                    Double.toString(totalExpenses) + "\nAllowable expenses for the trip: $" + 
                    Double.toString(allowedExpenses) + "\nAmount saved by the business person: $" + 
                    Double.toString((allowedExpenses - totalExpenses)));
        } // end try block
        
               catch(Exception e)
                {
                JOptionPane.showMessageDialog(null, "You must enter a number in all fields. Please retry. "); 
                } // end catch block
        
        } // end of action performed method
    
    } // end of calc class
    
    public static void main(String args[])
            
    {
    new TravelExpenses();    
    } // end of main method
    
} // end of travel expenses demo clas




Is This A Good Question/Topic? 0
  • +

Replies To: Try Catch Block Help

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5150
  • View blog
  • Posts: 13,838
  • Joined: 18-April 07

Re: Try Catch Block Help

Posted 27 January 2018 - 08:25 PM

In order to trigger a catch in your try catch, you must throw an exception. Or your code in the try catch must throw an exception. No where in your code do I see you throwing an exception. Did you mean to put your Double.tryParse statements inside your try? Right now they are before the try so they are not going to be caught by the try catch. Move lines 115 through 122 down past the try on line 124.

:)
Was This Post Helpful? 1
  • +
  • -

#3 bman8932  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 17

Re: Try Catch Block Help

Posted 28 January 2018 - 11:19 AM

Thanks! That got it to work! I couldn't find which lines of code were producing the error.

This post has been edited by ndc85430: 28 January 2018 - 11:40 AM
Reason for edit:: Removed quote of previous post. Please just press "Reply" in future.

Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,981
  • Joined: 25-December 13

Re: Try Catch Block Help

Posted 28 January 2018 - 11:30 AM

Quote

find which lines of code were producing the error.

Add a call to e.printStackTrace() in the catch block to see where the error happened.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1