Question

I want to make a data validation on a Popup My problem is that I can not check the input if I have made ​​a mistake.

the aim is, if the key.lenght not =0, the popup come.

I know this because I can controle one time my variable key

I do not understand how I could do to be able to control it once in the loop.

thank you

    final JFrame popup = new JFrame();
    boolean flag = false;


    String key = JOptionPane.showInputDialog(popup, "Enter key", null);
    char[] var = key.toCharArray();


    while (flag == false) {
        if (key.length() == 1) {
            flag = true;
        } else {
  --->      String popup2 = JOptionPane.showInputDialog(popup, "Enter key", null);
        }

    }
Was it helpful?

Solution

try with

key = JOptionPane.showInputDialog(popup, "Enter key", null);

instead of

String popup2 = JOptionPane.showInputDialog(popup, "Enter key", null);

You are not updating the key again.


--EDIT--

Try this one also

String key = null;
do {
    key = JOptionPane.showInputDialog(popup, "Enter key", null);
} while (key.length() != 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top