问题
I need to check users input. I have a menu and I need the user to select numbers 0-4 but if the user selects a letter instead of a number then I just get a InputMismatchException. So I am trying to validate that the user entered a number. Here is my code:
public class TestBankAccount {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
ArrayList<BankAccount> list = new ArrayList<BankAccount>();
int choice;
do {
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.println("4. Create new account");
System.out.print("Your choice, 0 to quit: ");
choice = input.nextInt();
switch (choice) {
case 1:
depositMoney(list);
break;
case 2:
withdrawMoney(list);
break;
case 3:
checkBalance(list);
break;
case 4:
createNewAccount(list);
break;
case 0:
System.out.println("Thank you for trusting us with your banking needs!");
break;
default:
System.out.println("Invalid option is selected!");
}
System.out.println();
} while (choice != 0);
if (list.size() > 0) {
displayResults(list);
}
}
I was thinking to do something like while (choice != 0 && choice != input.hasNextInt()); but I get an error. Any ideas?
回答1:
You can do somehting like this :
int choice = 0 ;
try{
choice = Integer.parseInt(input.next());
}
catch(NumberFormatException e)
{
System.out.println("invalid value enetered");
}
// Now you can check if option selected is between 1 & 4
// and throw some custom exception
回答2:
Either catch the exception and handle it, or instead of Scanner use
(char) System.in.read();
to receive characters. In this way you can avoid handling exceptions, which takes a lot of time. You can work on chars instead of integers then or check their validity and convert them to integers in this way:
int x = Character.getNumericValue(choice);
回答3:
Just wrap it in a try catch
do{
try{
System.out.println(choices)
choice = input.nextInt()
switch(choice){
....
}
}
catch(InputMismatchException){
System.out.println("Please enter a valid input")
}
}
while(whatever)
回答4:
I found the answer out. This is how I would validate the account number's.
int number;
while(true){
System.out.print("\nEnter account number: ");
try{
number = input.nextInt();
break;
}catch(Exception e){
System.err.println("Error: Invalid Entry! Please try only Integers");
input=new Scanner(System.in);
}
}
and this is how I would validate the menu item selected is a number and not a letter:
int choice = 0;
do {
while(true)
{
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.println("4. Create new account");
System.out.print("Your choice, 0 to quit: ");
try{
choice = Integer.parseInt(input.next());
break;
}
catch(Exception e){
System.err.println("Error: Invalid entry! Please Try Again!");
input=new Scanner(System.in);
continue;
}
}
来源:https://stackoverflow.com/questions/23787491/checking-user-input-is-a-number