0

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.

juiceb0xk
  • 949
  • 3
  • 19
  • 46
  • Why are you comparing the result from the dialog with `JOptionPane.CLOSED_OPTION`? You could also check if the String is a number without prarsing it as in and thereby circumvent the problematic `NumberFormatException`. See http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java – hotzst Dec 26 '16 at 07:48
  • I'm not sure how to handle if the user clicks the close buttons. I've looked everywhere and am unable to find a solution. I will check out the solution for the integer handling though, thanks for that. I also have two JOptionPanes running aswell, and I'm only wanting to close the one open at the time. – juiceb0xk Dec 26 '16 at 07:52

1 Answers1

0

You can do with this:

String code = JOptionPane.showInputDialog(null, "Your current holdings are: "
            + currentHoldings, "Edit Holdings", JOptionPane.CANCEL_OPTION);

    if (code == null){ //cancel button or "x" button

    }
    else if ("".equals(code)){ //don't input anything and click ok

    }
    else {
        //parse code to int and do what you want

    }
Viet
  • 3,349
  • 1
  • 16
  • 35