简体   繁体   中英

How do I keep asking a user for a valid integer input if there is more than one constraint to the input

I am trying to find a way to give some constraints as to what a user can input. More specifically, the input has to respect certain limits within my program. also, I want to continue asking the user for a valid input as long as he does not give an integer respecting the limits. I think my main point is to catch the InputMismatchException (if the user inputs a string for example) and to re-ask for an input if ever the integer inputs are not within the limits. I am confused as to how to use the try catch method. Any help would be appreciated! thank you.

Scanner sc = new Scanner(System.in);
System.out.println("Please enter your next move: ");
int rowNum = sc.nextInt();
int columnNum = sc.nextInt();
try {
    while (((rowNum < 0)||(columnNum < 0)) || ((rowNum >= Board.length)||(columnNum >= Board[0].length))) {
        System.out.println("Your move is not valid, Please enter a valid move");    
        rowNum = sc.nextInt();
        columnNum = sc.nextInt();
    }
    while (Board[rowNum][columnNum] != ' '){
        System.out.println("Your move is not valid, Please enter a valid move");
        rowNum = sc.nextInt();
        columnNum = sc.nextInt();
    }
} catch(Exception e) {
        System.out.println("Your move is not valid, please enter a valid move");
        sc.nextInt();
}

My method for doing this generally involves a do while loop . I usually end up with something like this:

boolean valid = false;
int input = -1;
Scanner in = new Scanner(System.in);
do{
    System.out.println("Please enter a number bigger than 5: ");
    String val = in.nextLine();
    if(val.matches("\\d+"))
        input = Integer.parseInt(val);

    if(input ==(int)input && input > 5)
        valid = true;
}while(!valid);

System.out.print(input + " is greater than 5.");

To check to see if the value is an integer I used the code found at this [post] an pointed out in the other answer, this does not check to see if it is bigger than an int which may be an issue.

This allows you to add as many if statements as you wish to check as many constraints as you need!

You might not need a try-catch for this. You could do something like setting a boolean variable when you get correct form of output and get out of the loop :

Scanner sc = new Scanner(System.in);
System.out.println("Please enter your next move: ");
int rowNum;
int columnNum;
boolean validInput = false;    
while(!validInput) {
    rowNum = sc.nextInt();
    columnNum = sc.nextInt();
    if(rowNum >= 0 && columnNum >= 0 && rowNum < Board.length && columnNum < Board[0].length && Board[rowNum][columnNum] == ' ') {
        validInput = true;
    }
}

You could also implement a cleaner code if you make a method that says that the input is valid or not which can be something like this:

boolean validateInput(int rowNum, int columnNum, int rowBound, int columnBound, char c) {
    return rowNum >= 0 && columnNum >= 0 && rowNum < rowBound && columnNum < columnBound && c == ' ';
}

// In the main program
do {
    // get user input
}while(validateInput(...))

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