2012-09-24 85 views
1

我有一個小型ATM程序,我正在使用異常處理。我在我的程序開始時嘗試着將它發送給程序底部的異常處理。用戶輸入長度的異常處理

用戶輸入一個5位數字...例如:12345.然後它檢查號碼,我檢查用戶號碼是否爲< 5然後發送錯誤但我想要它要做的是檢查長度是<或> 5以上然後發送錯誤。這是我的代碼,現在我的程序...

import java.util.ArrayList; 
import java.util.Scanner; 

    public class BankMain 
    { 
    private double availableBal =80; 
    private double totalBal =100; 
    private double availableBal2 =480; 
    private double totalBal2 =500; 
    private double availableBal3 =80; 
    private double totalBal3 =100; 
    ArrayList<Integer> cardNum = new ArrayList<Integer>(); 
    static Scanner input = new Scanner(System.in); 


    private String error;   //String the error from the exception 
    { 
     error = "error"; 
    } 


    public void cardNumbers(){ 


      Scanner cards = new Scanner(System.in); 
      Scanner input = new Scanner(System.in); 
      Scanner keyboard = new Scanner(System.in); 





      try{ 


      System.out.println("Please select a 5 digit card number"); 

      cardNum.add(input.nextInt()); 
      checkNumber(); 


      } 

      catch(invalidNumber err){ 

       System.out.println("Caught Error: " + err.getError()); 
      } 

      System.out.println("Thank you! You're card number is " +cardNum); 

      System.out.println("Type 'c' to go back to main menu."); 
      String value = keyboard.next(); 

      if(value.equalsIgnoreCase("c")){ 
       menu(); 

    } 

    else if (!keyboard.equals('c')){ 

     System.out.println("Invalid Entry!"); 

    } 

} 


    public void menu(){ 

    System.out.println("ATM Menu:"); 
    System.out.println(); 
    System.out.println("1 = Create Account"); 
    System.out.println("2 = Account Login"); 
    System.out.println("3 = Exit ATM"); 
    query(); 
       } 

     public void startAtm() 
     { 
      menu(); 

     } 



     public void drawMainMenu() 
     { 

      AccountMain main3 = new AccountMain(); 
      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(); 

      switch(selection) 
      { 
      case 1: 
       viewAccountInfo(); 
       break; 
      case 2: 
       withdraw(); 
       break; 
      case 3: 
       addFunds(); 
       break; 
      case 4: 
       AccountMain.selectAccount(); 
       break; 
      case 5: 
       System.out.println("Thank you for using this ATM!!! goodbye"); 
      } 
     } 

     public void viewAccountInfo() 
     { 
      System.out.println("Account Information:"); 
      System.out.println("\t--Total balance: $"+totalBal); 
      System.out.println("\t--Available balance: $"+availableBal); 
      drawMainMenu(); 
     } 

     public void viewAccountInfo2() 
     { 
      System.out.println("Account Information:"); 
      System.out.println("\t--Total balance: $"+totalBal2); 
      System.out.println("\t--Available balance: $"+availableBal2); 
      drawMainMenu(); 
     } 

     public void deposit(int depAmount) 
     { 
      System.out.println("\n***Please insert your money now...***"); 
      totalBal =totalBal +depAmount; 
      availableBal =availableBal +depAmount; 
     } 

     public void checkNsf(int withdrawAmount) 
     { 
      if(totalBal -withdrawAmount < 0) 
       System.out.println("\n***ERROR!!! Insufficient funds in you accout***"); 
      else 
      { 
       totalBal =totalBal -withdrawAmount; 
       availableBal =availableBal -withdrawAmount; 
       System.out.println("\n***Please take your money now...***"); 
       } 
     } 

     public void addFunds() 
     { 
      int addSelection; 

      System.out.println("Deposit funds:"); 
      System.out.println("1 - $20"); 
      System.out.println("2 - $40"); 
      System.out.println("3 - $60"); 
      System.out.println("4 - $100"); 
      System.out.println("5 - Back to main menu"); 
      System.out.print("Choice: "); 
      addSelection =input.nextInt(); 

      switch(addSelection) 
      { 
      case 1: 
       deposit(20); 
       drawMainMenu(); 
       break; 
      case 2: 
       deposit(40); 
       drawMainMenu(); 
       break; 
      case 3: 
       deposit(60); 
       drawMainMenu(); 
       break; 
      case 4: 
       deposit(100); 
       drawMainMenu(); 
       break; 
      case 5: 
       drawMainMenu(); 
       break; 
      } 
     } 

     public void withdraw() 
     { 
      int withdrawSelection; 

      System.out.println("Withdraw money:"); 
      System.out.println("1 - $20"); 
      System.out.println("2 - $40"); 
      System.out.println("3 - $60"); 
      System.out.println("4 - $100"); 
      System.out.println("5 - Back to main menu"); 
      System.out.print("Choice: "); 
      withdrawSelection =input.nextInt(); 

      switch(withdrawSelection) 
      { 
      case 1: 
       checkNsf(20); 
       drawMainMenu(); 
       break; 
      case 2: 
       checkNsf(40); 
       drawMainMenu(); 
       break; 
      case 3: 
       checkNsf(60); 
       drawMainMenu(); 
       break; 
      case 4: 
       checkNsf(100); 
       drawMainMenu(); 
       break; 
      case 5: 
       drawMainMenu(); 
       break; 
      } 
     } 

public void query(){ 

    Scanner keyboard = new Scanner(System.in); 
    double input = keyboard.nextInt(); 


    if (input == 2){ 

     BankMainPart2 main2 = new BankMainPart2(); 

     System.out.println("Please enter your 5 digit card number."); 
     BankMainPart2.loginCard(cardNum); 



    } 
    else if (input == 1){ 

     cardNumbers(); 

    } 

    else if (input == 3){ 
     System.out.println("Thank you, have a nice day!"); 
     System.exit(0); 


    } 

} 

public void checkingMenu() 
{ 

    AccountMain main3 = new AccountMain(); 
    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(); 

    switch(selection) 
    { 
    case 1: 
     viewAccountInfo2(); 
     break; 
    case 2: 
     withdraw(); 
     break; 
    case 3: 
     addFunds(); 
     break; 
    case 4: 
     AccountMain.selectAccount(); 
     break; 
    case 5: 
     System.out.println("Thank you for using this ATM!!! goodbye"); 
    } 
} 

private static void checkNumber() throws invalidNumber    //run the check activation exception 
{ 
    if(input.nextInt() == 5) 
    { 
     System.out.println("Works!"); 
    } 
    else{ 

    throw new invalidNumber("invalid number"); 
} 
} 

    public static void main(String args[]) 
    { 
     BankMain myAtm = new BankMain(); 
     myAtm.startAtm(); 
    } 
} 

回答

1

數轉換爲字符串,然後使用length()檢查數字

+0

感謝您的答覆,對不起,我是新來的Java,我不知道到底該怎麼做 –

+1

使用['將String.valueOf()'](HTTP://文檔。 oracle.com/javase/6/docs/api/java/lang/String.html#valueOf%28int%29) –

+0

不知道如何使用這個 –

0

你需要得到多少,如果整個代碼,然後檢查一下它的長度== 5與否,您需要使用的輸入轉換爲字符串:

String.valueOf(int i) 

Integer.toString(int i) 

或者只是用它讀成字符串:

string givenCode = yourScan.next(); 

然後檢查長度:

if (givenCode.length() == 5) 
1

正因爲如此,你的程序應該掛(你是不是在你的問題中指定這讓我的假設可能不正確)。您打電話給input.nextInt()兩次,這會使您的程序期望您的用戶有兩組數字。

爲了解決這個問題,我建議您首先使用nextLine()方法將用戶的輸入作爲字符串讀取。然後,你會做一些像這樣:

String userInput = input.nextLine().trim(); 
checkNumber(userInput); 
cardNum.add(Integer.parseInt(userInput); 

此外,checkNumber,由於是將失敗,因爲我們現在正在閱讀整條生產線,所以你將不得不作出以下修改:

  • 你將不得不遍歷你的角色,並使用isDigit,以確保您的每一個字符是數字

  • 用戶排序0的正則表達式以確保字符串完全由5個數字組成。對於正則表達式,您可以查看this教程,以幫助您走上正確的道路。如果你是初學者,這可能看起來有點壓倒性,但是正則表達式是,通常建議使用來對字符串進行數據驗證。

1
 System.out.println("Please select a 5 digit card number"); 

     String s=input.next(); 

     //Here check the nu. digits by using length() 

     cardNum.add(s); 

     checkNumber(); 
+0

這不起作用 –

+0

有什麼問題..... – 2012-09-24 05:26:45

+0

說,當我使用此代碼 –