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

public class TravelExpenses extends JPanel
  private JTextField days;           //declaring textfields and labels
  private JTextField lodging;
  private JTextField airfare;
  private JTextField taxiCost;
  private JTextField miles;
  private JTextField rental;
  private JTextField seminarFees; 
  private JTextField parkingFees;
  private JLabel daysL;
  private JLabel lodgingL;
  private JLabel airfareL;
  private JLabel taxiCostL;
  private JLabel milesL;
  private JLabel rentalL;
  private JLabel seminarFeesL;
  private JLabel parkingFeesL; 
String input;
//int day;
double day, lodge,air,taxi,mile,rentals,sem,park;
  public TravelExpenses()
     setLayout(new GridLayout(8,1 ));
     days = new JTextField(4);
     lodging = new JTextField(4);
     airfare = new JTextField(4);
     taxiCost = new JTextField(4);
     miles = new JTextField(4);
     seminarFees = new JTextField(4);
     parkingFees = new JTextField(4);
  	  rental = new JTextField(4);  

	  daysL= new JLabel("Duration of the Trip");
	  lodgingL = new JLabel("Lodging Cost");
	  airfareL = new JLabel("Airfare Cost");
	  taxiCostL = new JLabel("Taxi Expenses");
     milesL = new JLabel("Miles Driven");
     seminarFeesL= new JLabel("Seminar Fees");
     parkingFeesL= new JLabel("Parking Fees");
     rentalL = new JLabel("Rental Cost");
	  input = days.getText();
     day = Double.parseDouble(input);
	  input = lodging.getText();
	  lodge = Double.parseDouble(input); 
	  input = airfare.getText();
	  air = Double.parseDouble(input);
	  input = taxiCost.getText();
	  taxi = Double.parseDouble(input);
	  input = miles.getText();
	  mile = Double.parseDouble(input);
	  input = rental.getText();
	  rentals = Double.parseDouble(input);
	  input = seminarFees.getText();
	  sem = Double.parseDouble(input);
	  input = parkingFees.getText();
	  park = Double.parseDouble(input);
 }//end of constructor
  public void setValues(int d,double l,double a,double t, double m,double r,double s,double p)
  day = d;
  lodge = l;

  air = a;
  taxi = t;
  mile = m;
  rentals = r;
  sem = s;
  park = p;

public double getDays()
   return day;
public double getLodging()
   return lodge;
public double getAirfare()
  return air;
public double getTaxiCost()
  return taxi;
public double getMiles()
  return mile;
public double getRentalCost()
  return rentals;
 public double getSeminarFees()
   return  sem;
public double getParkingFees()
  return park;


Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(
at java.lang.Double.parseDouble(
at TravelExpenses.<init>(
at TravelGUI.<init>(
at tets.main(
I am having problems with this code i can get the window and everything else to work. however, when i try to parse the input from the JTextField for the days i get the above error and i cant figure out whats causing it.
please help
thanks in advance

Hi ngnt4,

input = days.getText();
day = Double.parseDouble(input);

The value returned by getText() method is an empty string(and not a parsable string), this is why you get the exception.
You can fix this by initializing the text field before calling the getText() method


Anyway I think you should move the parsing of text fields values in another method, and call it only when you need the values....

You could also (more correctly) fix it by using error checking. try... catch. Exception handling. Look them up. The poster above me, his solution won't work if a user goes back and edits something into a text field that isn't a double. Then you'll get the error again. Error checking will always work.

Edited 7 Years Ago by BestJewSinceJC: n/a

sweet thanks a lot...i was trying to figure out for a long time but as you can see im kinda new to this....thanks again i will look into both things

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