检查用户输入是一个数字

时间:2014-05-21 15:21:23

标签: java java.util.scanner

我需要检查用户输入。我有一个菜单,我需要用户选择数字0-4,但如果用户选择一个字母而不是一个数字,那么我只是得到一个InputMismatchException。所以我试图验证用户输入了一个数字。这是我的代码:

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);
    }
}

我当时想要做些什么(选择!= 0&amp;&amp; choice!= input.hasNextInt());但是我收到了一个错误。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

您可以这样做:

 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

答案 1 :(得分:0)

捕获异常并处理它,或者代替Scanner使用

(char) System.in.read();

接收字符。通过这种方式,您可以避免处理异常,这需要花费大量时间。您可以使用字符而不是整数,或者检查它们的有效性并以这种方式将它们转换为整数:

int x = Character.getNumericValue(choice);

答案 2 :(得分:0)

将其包装在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)

答案 3 :(得分:-1)

我找到了答案。这就是我验证帐号的方法。

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);
        }
    }

这就是我如何验证所选菜单项是一个数字而不是一个字母:

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;
            }
    }