2012-09-27 66 views
2

這是我正在開發的程序的一小部分。我正在嘗試檢查用戶是否輸入了正確的號碼。用戶輸入驗證有問題

他們有五種選擇可供選擇,因此他們可以選擇1,2,3,4或5,然後按回車鍵。

所以我想檢查一下,以確保用戶沒有在< 1或> 5中輸入任何內容。我得到那部分工作...但我只想知道是否有更簡單的方法來做到這一點然後從我在下面的代碼中做的。

下一部分是我也想確保用戶不鍵入字母。像「gfgfadggdagdsg」作爲選擇。

這裏是我的,我工作的一部分的代碼....

public void businessAccount() 
    { 


     int selection; 

     System.out.println("\nATM main menu:"); 
     System.out.println("1 - View account balance"); 
     System.out.println("2 - Withdraw funds"); 
     System.out.println("3 - Add funds"); 
     System.out.println("4 - Back to Account Menu"); 
     System.out.println("5 - Terminate transaction"); 
     System.out.print("Choice: "); 
     selection = input.nextInt(); 

      if (selection > 5){ 

      System.out.println("Invalid choice."); 
      businessAccount(); 

     } 
      else if (selection < 1){ 
       System.out.println("Invalid choice."); 
       businessAccount(); 
      } 
      else { 

     switch(selection) 
     { 
     case 1: 
      viewAccountInfo3(); 
      break; 
     case 2: 
      withdraw3(); 
      break; 
     case 3: 
      addFunds3(); 
      break; 
     case 4: 
      AccountMain.selectAccount(); 
      break; 
     case 5: 
      System.out.println("Thank you for using this ATM!!! goodbye"); 
     } 
      } 
    } 
+0

此代碼是確定的。你可以跳過檢查'< 1' and '> 5''通過默認情況下打印'無效輸入' – Nishant

+0

小心遞歸,你可以讓自己陷入困境,如果你不習慣使用它,最終會出現一些奇怪的錯誤。例如,在您的代碼中,當您從businessAccount中調用businessAccount時,如果任何代碼存在於您的else語句之外,它將運行多次,並且上次運行的時間不會是您期待的時間。現在你可以,但是如果你將來編輯這個方法,你可能會遇到意想不到的行爲。 – Scott

+1

我還建議在switch語句的底部添加'default'來打印錯誤信息。 – Scott

回答

7

您可以通過添加default的情況來消除檢查< 1> 5

try{ 
    selection = input.nextInt();   
    switch(selection){ 
     case 1: 
      viewAccountInfo3(); 
      break; 
     case 2: 
      withdraw3(); 
      break; 
     case 3: 
      addFunds3(); 
      break; 
     case 4: 
      AccountMain.selectAccount(); 
      break; 
     case 5: 
      System.out.println("Thank you for using this ATM!!! goodbye"); 
      break; 
     default:    
      System.out.println("Invalid choice."); 
      businessAccount(); 

     } 
}catch(InputMismatchException e){ 
    //do whatever you wanted to do in case input is not an int 
} 
+0

感謝您的回覆,我如何檢查以確保用戶輸入數字而不是字符? –

+0

參考[this](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt%28int%29)捕獲'InputMismatchException',然後顯示錯誤消息。 – Nishant

1

的替代方案將使用正則表達式來得到它的工作。 假設你有一個字符串x然後

String x =「something」;

如果(x.matches( 「正則表達式」)){

}

另一種方式來做到這一點的環繞嘗試捕捉。

0

使用BufferedReader你可以做這樣的事情:

InputStreamReader isr = new InputStreamReader(System.in); 
BufferedReader br = new BufferedReader(isr); 
String s = br.readLine(); 
int selection = 0; 

try{ 
    selection = Integer.parseInt(s); 
    if(selection > 5 || selection < 1){ 
     System.out.println("Invalid choice."); 
     businessAccount(); 
    }else{ 
     // your switch code here 
    } 
    // you can use @Nishant's switch code here. it is obviously better: using switch's default case. 
}catch(NumberFormatException ex){ 
    // throw new Exception("This is invalid input"); // or something like that.. 
    System.out.println("Invalid choice."); 
    businessAccount(); 
} 

希望有所幫助。

注意:您必須import java.lang.NumberFormatExceptionimport java.io.InputStreamReaderimport java.io.BufferedReader

+0

在br上發生錯誤。的ReadLine();說未處理IOEXCEPTION –

+0

你可以在當前catch塊之前或之前添加另一個catch(IOException ex){}'塊 – Prasanth

+0

不確定你的意思,對於java來說還是新的 –

0

使用開關的情況下它更好,更快的速度在if語句當你從一個特定的查詢選擇。