The input dialog will display the current stock for an item. I want the user to input the new holdings for an item via an integer.
If the user closes or cancels the input dialog WHILST also having nothing inside the input text field, I want to go back to the main menu. This will also happen even if the user has something in the input text field.
If the user enters in a string, a number format exception will be caught and the user will be prompted to enter in a number only.
If the input is empty, and the user clicks OK, a message dialog will show and say to the user that you will need to input a number.
Currently, I'm finding it very hard for me to implement these methods into my input dialog.
Here's my code:
int newInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Your current holdings are: "
+ currentHoldings, "Edit Holdings", JOptionPane.CANCEL_OPTION));
//Currently, when closing the program if newInput is empty
//It goes straight to the number format exception
//I need the program closed if the user closes it.
if (newInput == JOptionPane.CLOSED_OPTION)
{
JOptionPane.getRootFrame().dispose();
break;
}
...
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Please enter in a number!");
}
Even if the text field is empty and the user closes either by the red cross or cancel button, the number format exception still throws. I want the program to close even if there is nothing inside the text field.
How can I also check if the user has input anything into the input dialog text field? I'm currently finding it hard to implement this feature also.
My question is, how can I better manage my input dialog to handle the exit buttons if the user input is empty and or closes the input dialog.
Thank you for your help.