我正在尝试对以下测量进行长度转换:英尺,英寸,码,毫米和米。但出于某种原因,如果我脚下有东西JTextField
,我只能转换。如果我尝试再做一次,我会收到错误:
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)
...
我猜我的逻辑错误,我尝试了一些事情,但没有一个能奏效。那么,我需要做些什么来解决这个问题?
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);
}
}
}
答案 0 :(得分:1)
Double.parseDouble(ft)
调用FloatingDecimal.readJavaFormatString(ft).doubleValue()
里面显然不能使用空字符串,只需检查ft.isEmpty()
并提供敏感的默认输出。