简体   繁体   中英

Keep on prompting user after invalid input

Now I know that there is a thread called "Validating input using java.util.Scanner". I already looked there and that thread only answered 1/2 of my problems. The other half is when someone enters a number greater than 2 I get Array Index Out of Bounds Exception. I just need help on if someone enters a 3 for either row or column, the console should prompt something like this:

"Enter the coordinates to place an 'X'. Row then Column."
 //enters 3 and 3
"Please enter a valid input"

It would keep and asking the user for a valid number until he gives one.

Would I need to do something like the !keyboard.hasNextInt() but for integers? And that would run smoothly with the rest of my code?

You could use a do-while loop. Something like

do {
    //prompt
    //input
} while (input not valid);

Where prompt and input should be replaced by code to prompt the user and accept input. In the while section, check if input is valid.

You're question isn't too clear but I'll try to make sense of it.

I'm assuming you've named your scanner "keyboard"

Before I try running this code, the first problem I can see is this (Note that I grabbed this from your code before you edited the question):

 while (board[row][col] != ' ') 
{
    System.out.println("Already occupied space");
    System.out.println("Choose again");
    row = keyboard.nextInt();
    col = keyboard.nextInt();
}

Earlier, you made sure that the user enters integers. However, you have abandoned that completely in this case.

Assuming you're trying to avoid an error if the user enters something other than an integer, this is what I would do:

while(true){
        boolean valid = true;     

        if(!keyboard.hasNextInt()){
            valid = false;
            keyboard.next();
        }
        else{
            row = keyboard.nextInt();
        }
        if(!keyboard.hasNextInt()){
            valid = false;
            keyboard.next();
        }
        else{
            col = keyboard.nextInt();
        }
        if (valid && (row > 2 || col > 2)){
            System.out.println("Please enter a valid input");
            continue;
        }
        else if(!valid){
            System.out.println("Please enter a valid input");
            continue;
        }
        else
            break;  
    }

There are a couple reasons this code might seem a bit long. First off, we're trying to test if the input is an integer before we attempt to store it as an int. Secondly, we want to compare the input after we store it successfully to see if it's less than 3. If the input isn't an integer, the boolean "valid" will be false. The way a compiler works, if valid is false in the if statement it will ignore anything to the right of the &&, avoiding an error.

I admit, this is using some commands that I haven't learned before, so this might not be the most efficient way. But you get the idea :)

PS You should probably throw the above code into a method.

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