I am trying to have a length conversion for the following measurements: feet, inch, yard, millimeter, and meter. But for some reason I can only convert if I have something in my foot JTextField
. If I try to do another I get an error:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at SpecsProgram$LengthBttnHandler.actionPerformed(SpecsProgram.java:711)
...
I'm guessing my logic is wrong for the whole thing, I have tried a few things but none of them worked. So, what do I need to do to fix this?
class LengthBttnHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String ft, in, yd, mm, mt;
double foot, inch, yard, mill, met;
ft = footEText.getText();
if (ft!=null)
{
foot = Double.parseDouble(ft);
yard = foot/3;
yd = yard+"";
inch = foot*12;
in = inch+"";
met = foot*.3048;
mt = met+"";
mill = (foot*12)*25.4;
mm = mill+"";
yardEText.setText(yd);
inchEText.setText(in);
metEText.setText(mt);
millEText.setText(mm);
}
in = inchEText.getText();
if (in!=null)
{
//Look up how to truncate
inch = Double.parseDouble(in);
foot = inch/12;
ft = foot+"";
yard = inch/36;
yd = yard+"";
met = inch*0.0254;
mt = met+"";
mill = inch*25.4;
mm = mill+"";
footEText.setText(ft);
yardEText.setText(yd);
metEText.setText(mt);
millEText.setText(mm);
}
yd = yardEText.getText();
if (yd!=null)
{
yard = Double.parseDouble(yd);
foot = yard*3;
ft = foot+"";
inch = yard*36;
in = inch+"";
met = yard*.9144;
mt = met+"";
mill = yard*(36*25.4);
mm = mill+"";
footEText.setText(ft);
inchEText.setText(in);
metEText.setText(mt);
millEText.setText(mm);
}
mm = millEText.getText();
if (mm!=null)
{
mill = Double.parseDouble(mm);
foot = mill*.00328084;
ft = foot+"";
inch = mill*.0393701;
in = inch+"";
yard = mill*.00109361;
yd = yard+"";
met = mill*.001;
mt = met+"";
footEText.setText(ft);
inchEText.setText(in);
metEText.setText(mt);
yardEText.setText(yd);
}
mt = metEText.getText();
if (mt!=null)
{
met = Double.parseDouble(mt);
foot = met*3.28084;
ft = foot+"";
yard = met*1.09361;
yd = yard+"";
inch = met*39.3701;
in = inch+"";
mill = met*1000;
mm = mill+"";
footEText.setText(ft);
yardEText.setText(yd);
inchEText.setText(in);
millEText.setText(mm);
}
}
}