Попробуйте ввести значения в текстовое поле emp_text2
, и код вернет следующие значения соответственно:
""
, " "
, "1"
, "1.1"
, "-1.1"
, "1.0 "
возвращает 0.0
, 0.0
, 1.0
, 1.1
, -1.1
, 1.0
.
Что происходит, если ввод "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;
}