2014-05-21 2 views
0

사용자 입력을 확인해야합니다. 메뉴가 있는데 숫자 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 & & 선택 = input.hasNextInt()); 하지만 오류가 발생합니다. 어떤 아이디어?

+0

왜 그냥 예외를 catch하고 처리 :

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

는이 내가 숫자가 아니라 편지입니다 선택 메뉴 항목을 검증 할 방법입니까? –

답변

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 
0

는 어느 예외를 캐치하고 그것을 처리, 또는 그 대신 스캐너의 문자를받을 수

(char) System.in.read(); 

를 사용합니다. 이런 식으로 많은 시간이 걸리는 예외 처리를 피할 수 있습니다. 그런 다음 대신 정수의 문자에 일을하거나 자신의 유효성을 확인하고이 방법으로 정수로 변환 할 수 있습니다

int x = Character.getNumericValue(choice); 
0

그냥 당신이 같은 somehting을 할 수

do{ 
    try{ 
     System.out.println(choices) 
     choice = input.nextInt() 
     switch(choice){ 
     .... 
     } 

    } 
    catch(InputMismatchException){ 
     System.out.println("Please enter a valid input") 
    } 
} 
while(whatever) 
-1

답변을 찾았습니다. 이것이 계좌 번호를 확인하는 방법입니다.

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