2 回答

TA貢獻1815條經驗 獲得超6個贊
您可以使用此方法代替Double.parseDouble。
public static double tryParsDouble(String s, double defaultValue) {
if (s == null) return defaultValue;
try {
return Double.parseDouble(s);
} catch (NumberFormatException x) {
return defaultValue;
}
}
然后到:
double F = tryParsDouble(wop_txt5.getText(), 0.0);

TA貢獻1864條經驗 獲得超2個贊
嘗試在emp_text2文本字段中輸入值,代碼分別返回以下值: "", " ", "1", "1.1", "-1.1","1.0 "返回0.0, 0.0, 1.0, 1.1, -1.1, 1.0。
如果輸入是 會發(fā)生什么"1.1x"?這會拋出NumberFormatException- 應用程序需要弄清楚要做什么。
double value = getDoubleValue(emp_text2.getText());
...
private static double getDoubleValue(String input) {
double result = 0d;
if ((input == null) || input.trim().isEmpty()) {
return result;
}
try {
result = Double.parseDouble(input);
}
catch (NumberFormatException ex) {
// return result -or-
// rethrow the exception -or-
// whatever the application logic says
}
return result;
}
添加回答
舉報