简体   繁体   中英

loop while user input is incorrect using JOptionPane

I am trying to simulate a lottery game in java. Right now everything is working I am validating the user only enter number 1 through 100> I also want to prevent the user to enter and empty value. Right now is working using a try.. catch.. but it only works for the first time. The user enters 6 numbers. lets say the first input is blank then an error shows but if the user press enter again on an empty input the program crashes. I can not get it to loop I've tried several things with no luck. Here is the code where I get user's input.

String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
//get input 5 numbers from user 
for(int i=0; i<6;i++){

    //boolean correctInput = false;
    //while(!correctInput){
         try {
            inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                while(!validate(inputNumbers[i])){
                     JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                     inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                }
                ////check for duplicate entries from user
                for (int k=0; k<i; k++)  {    
                    while (k!=i && inputNumbers[k] == inputNumbers[i])  {
                        JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                        inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    }
                }
                //correctInput = true;
                //break;
            }catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null,"Number not entered! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                //throw new NumberFormatException("not number");
                inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                //correctInput = false;
                //JOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                //continue;

            //}
        }
        userNumbers[i] = inputNumbers[i];
    }

Trying a do/while

 boolean correctInput = false;
        ///create array to display user
        String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
        //get input 5 numbers from user 
        for(int i=0; i<6;i++){
            do {
                try {
                    inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    while(!validate(inputNumbers[i])){
                        JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                        inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    }

                    ////check for duplicate entries from user
                    for (int k=0; k<i; k++)  {    
                        while (k!=i && inputNumbers[k] == inputNumbers[i])  {
                            JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                            inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                        }
                    }
                    correctInput = true;
                    //break;
                }catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null,"Number not entered! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                    //throw new NumberFormatException("not number");
                    inputNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100"));
                    correctInput = false;
                    //JOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                    //continue;

                }
            }while(!correctInput);
            userNumbers[i] = inputNumbers[i];
        }

The try/catch should be inside the while loop, otherwise you exit the loop when an exception is thrown.

Hint: do/while.

Here is your code modified to make much more simple to loop through the 6 numbers, I have tested it and it works fine. Let me know if something is not clear

   String [] charNums = {"1st","2nd","3rd","4th","5th","Bonus"};
   int[] inputNumbers = new int[6];
   //get input 6 numbers from user 
   int cnt = 0;
   while(true)
   {
     try
     {
       String tmp = JOptionPane.showInputDialog("Enter "+charNums[cnt]+" number from 1 to 100");
       int val = Integer.parseInt(tmp);
       boolean duplicate = false;
       for (int k=0; k<cnt; k++)
       {
         if(val==inputNumbers[k])
         {
           JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
           duplicate = true;
           break;
         }   
       }   
       if(duplicate)continue;

       inputNumbers[cnt] = val;
       cnt++;
     }
     catch(Exception e)
     {
     }
     if(cnt==6)break;
   }

the code above didn't work for me. But I fixed by putting the try and catch inside the validate method now it's looping every time there is a wrong input. here is the code

public static boolean validate(String num){

    try {
        int convertedNum = Integer.parseInt(num);
        if(convertedNum < 0 || convertedNum > 100){
            return false;
        }else{
            return true;
        }
    }catch ( NumberFormatException e){
        return false;
    }
}

for(int i=0; i<6;i++){

            inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
            while(!validate(inputNumbers[i])){
                if(inputNumbers[i] == null){
                    int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION);
                    if(stopGame == JOptionPane.YES_OPTION){
                        System.exit(0);
                    }
                }
                JOptionPane.showMessageDialog(null,"Invalid Number! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
                inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
            }

            ////check for duplicate entries from user
            for (int k=0; k<i; k++)  {    
                while (k!=i && Integer.parseInt(inputNumbers[k]) == Integer.parseInt(inputNumbers[i]))  {
                    JOptionPane.showMessageDialog(null,"Duplicate Entry! try again","ERROR",JOptionPane.ERROR_MESSAGE);
                    inputNumbers[i] = JOptionPane.showInputDialog("Enter "+charNums[i]+" number from 1 to 100");
                }
            }

            //convert string to int
            userNumbers[i] = Integer.parseInt(inputNumbers[i]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM