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;
 /**
 Constructorr
 */
  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);  
	  setBorder(BorderFactory.createTitledBorder("Expenses"));


	  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");
     add(daysL);
	  add(days);
	  add(lodgingL);
	  add(lodging);
	  add(airfareL);
	  add(airfare);
	  add(taxiCostL);
	  add(taxiCost);
	  add(milesL);
	  add(miles);
	  add(rentalL);
	  add(rental);
	  add(seminarFeesL);
	  add(seminarFees);
	  add(parkingFeesL);
	  add(parkingFees);
     
	  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(FloatingDecimal.java:994)
at java.lang.Double.parseDouble(Double.java:510)
at TravelExpenses.<init>(TravelExpenses.java:69)
at TravelGUI.<init>(TravelGUI.java:24)
at tets.main(tets.java:5)
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

Recommended Answers

All 3 Replies

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

days.setText("0");

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.