0

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);
           }
       }
   }
Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
Jon
  • 31
  • 1
  • 5
  • my guess is you want if (ft != null && ft.length() > 0) for each of your if's and their conditions (respective to the variable they evaluate). – Mark W Jan 17 '14 at 21:31
  • Best to use a [`JSpinner`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JSpinner.html) with a [`SpinnerNumberModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SpinnerNumberModel.html) for these types of tasks. E.G. as seen [here](http://stackoverflow.com/a/10021773/418556). – Andrew Thompson Jan 17 '14 at 23:31

1 Answers1

1

Double.parseDouble(ft) invokes FloatingDecimal.readJavaFormatString(ft).doubleValue() inside which apparently doesn't work with empty strings, just write a check for ft.isEmpty() and provide a sensitive default output.

aljipa
  • 716
  • 4
  • 6