dimanche 29 mars 2015

Java Please help. So many Errors I don't know what I'm doing wrong but apparently its a lot



You'll Just have to run it yourself everything goes wrong when I hit calculate.


This is all the errors I get:



Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at java.lang.Double.valueOf(Unknown Source)
at school.TravelExpenses$CalcButtonListener.actionPerformed(TravelExpenses.java:139)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


Code:



package school;

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

/**
* The TravelExpense class creates the GUI for the Travel Expenses application.
*/

public class TravelExpenses extends JFrame {
// The following variables will reference the
// custom panel objects
private JPanel travelInfoPanel; // TravelInfo panel
private JPanel buttonPanel; // Buttons panel

// Labels for the Travel Information fields.
private JLabel DaysLabel;
private JLabel AirfareLabel;
private JLabel CarLabel;
private JLabel MilesLabel;
private JLabel ParkingLabel;
private JLabel CabLabel;
private JLabel RegistrLabel;
private JLabel LodgingLabel;

// Text Fields for Travel Information entry
private JTextField DaysTextField;
private JTextField AirfareTextField;
private JTextField CarTextField;
private JTextField MilesTextField;
private JTextField ParkingTextField;
private JTextField CabTextField;
private JTextField RegistrTextField;
private JTextField LodgingTextField;

private JButton calcButton;

private double mealsReimbursed = 37.00;
private double parkingReimbursed = 10.00;
private double taxiChargesReimbursed = 20.00;
private double lodgingChargesReimbursed = 95.00;
private double perMileReimbursed = 0.27;
public TravelExpenses() {
super("Travel Expenses");

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

buildTravelInfoPanel();
buildButtonPanel();

add(travelInfoPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);

pack();
setVisible(true);
}


private void buildTravelInfoPanel() {
DaysLabel = new JLabel("Number of days on trip: ");
AirfareLabel = new JLabel("Amount of airfare: ");
CarLabel = new JLabel("Amount of car rental: ");
MilesLabel = new JLabel("Miles driven: ");
ParkingLabel = new JLabel("Parking Fees: ");
CabLabel = new JLabel("Taxi fees: ");
RegistrLabel = new JLabel("Conference registration: ");
LodgingLabel = new JLabel("Lodging charges per night: ");

DaysTextField = new JTextField(3);
AirfareTextField = new JTextField(8);
CarTextField = new JTextField(8);
MilesTextField = new JTextField(4);
ParkingTextField = new JTextField(6);
CabTextField = new JTextField(6);
RegistrTextField = new JTextField(8);
LodgingTextField = new JTextField(6);

travelInfoPanel = new JPanel();

travelInfoPanel.setLayout(new GridLayout(10, 2));

travelInfoPanel.add(DaysLabel);
travelInfoPanel.add(DaysTextField);
travelInfoPanel.add(AirfareLabel);
travelInfoPanel.add(AirfareTextField);
travelInfoPanel.add(CarLabel);
travelInfoPanel.add(CarTextField);
travelInfoPanel.add(MilesLabel);
travelInfoPanel.add(MilesTextField);
travelInfoPanel.add(ParkingLabel);
travelInfoPanel.add(ParkingTextField);
travelInfoPanel.add(CabLabel);
travelInfoPanel.add(CabTextField);
travelInfoPanel.add(RegistrLabel);
travelInfoPanel.add(RegistrTextField);
travelInfoPanel.add(LodgingLabel);
travelInfoPanel.add(LodgingTextField);

// Add an empty border around the panel for spacing.
travelInfoPanel.setBorder(BorderFactory
.createEmptyBorder(10, 10, 1, 10));
}

private void buildButtonPanel() {
// Create the calcButton.
calcButton = new JButton("Calculate");

calcButton.addActionListener(new CalcButtonListener());

buttonPanel = new JPanel();

buttonPanel.setLayout(new BorderLayout(5, 5));

// Add the two buttons to the buttonPanel.
buttonPanel.add(calcButton, BorderLayout.CENTER);

buttonPanel.setBorder(BorderFactory.createEmptyBorder(1, 10, 10, 10));
}

private class CalcButtonListener implements ActionListener {

String days = DaysTextField.getText();
String air = AirfareTextField.getText();
String carRental = CarTextField.getText();
String miles = MilesTextField.getText();
String parking =ParkingTextField.getText();
String taxi = CabTextField.getText();
String Registr = RegistrTextField.getText();
String lodging = LodgingTextField.getText();

public void actionPerformed(ActionEvent e) {
// Declare variables for calculated items
double actualExpenses=Double.valueOf(air) + Double.valueOf(carRental) + Double.valueOf(parking) + Double.valueOf(taxi) + Double.valueOf(Registr) + Double.valueOf(lodging) + (Double.valueOf(miles)*.27);
double allowableExpenses=(mealsReimbursed+parkingReimbursed+taxiChargesReimbursed+lodgingChargesReimbursed)*Integer.valueOf(days)+perMileReimbursed;
double excessAmount=actualExpenses-allowableExpenses;
String savedMoney="Owed";
if(excessAmount<0){
savedMoney="Saved";
excessAmount*=-1;
}
JOptionPane.showMessageDialog(null, "Total Expenses: "+actualExpenses+"\nAllowable Expenses: "+allowableExpenses+"\n"+"Money "+savedMoney+": "+excessAmount);
}
}

public static void main(String[] args) {
new TravelExpenses();
}
}



Aucun commentaire:

Enregistrer un commentaire