У меня есть JTextField, которому я не всегда передаю значение, но это вызывает ошибку NumberFormatException при запуске программы, если человек ничего не ввел в JTextField.У меня есть код ниже, что я пытаюсь преобразовать нулевой ответ в другое значение String.Почему он не присваивает stringInput в виде строки «0»?
public int getOptionalDinners()
{
try
{
// get the input from the text field and determine if it's more than 0
String stringInput = " ";
stringInput = dinnerTextField.getText();
if (stringInput == null)
stringInput = "0";
int validAmount = Integer.parseInt(stringInput);
if (validAmount < 0)
throw new IllegalArgumentException();
dinnerQuantity = validAmount * 30;
}
catch (NumberFormatException error)
{
JOptionPane.showMessageDialog (null, "The number of dinners needs to be numeric.",
"Input Error", JOptionPane.ERROR_MESSAGE);
}
catch (IllegalArgumentException error)
{
JOptionPane.showMessageDialog (null, "The number of dinners needs to be higher than 0.",
"Input Error", JOptionPane.ERROR_MESSAGE);
}
return dinnerQuantity;
}