2015-02-10 57 views
-2

我的任務有一種方法出現問題。這是此方法的要求:數組未顯示(Java)

「此方法使用循環列出陣列中包含的所有帳戶,將每個帳戶詳細信息添加到字符串,然後以下面屏幕截圖中指定的格式輸出到屏幕。通過檢查每個數組插槽是否有一個帳戶對象,然後將其詳細信息添加到輸出String(arrayname [index]!= null),確保沒有越界異常「

這是我的代碼方法:

public void listAllAccounts() 
{ 
    String allAccountsString = "List of all accounts: \n"; 

    for(int i = 0; i < ACCOUNT_SPACES; i++) 
    { 
     //allAccountsString += accountArray[numAccounts]; 
     if (accountArray[i] !=null) 
     { 
      allAccountsString += accountArray[i].toString() + "\n\n" ; 
     } 
    } 
    JOptionPane.showMessageDialog(null, allAccountsString); 

問題是,消息對話框不顯示我已經創建的帳戶。它只是顯示"List of all accounts: \n";

任何想法?

這是整個類的代碼:

public class MyBankController 
{ 
    /** 
    * Variables that will be used by this class 
    */ 

    private BankAccount newAccount; 
    private BankAccount accountArray[]; 
    int numAccounts = 0; 
    int ACCOUNT_SPACES = 2; 
    private boolean accountStatus = false; 

    /** 
    * Constructor for objects of class MyBankController - to be left empty by requirements. 
    */ 
    public MyBankController() 
    { 
     // 
    } 

    /** 
    * A method to create a new account, accepting user input and allocating memory space. 
    */ 
    public void createAccount(String customerName, int accountNumber) 
    { 
     newAccount = new BankAccount(customerName, accountNumber); 
     accountArray = new BankAccount [2]; 
     if(numAccounts +1 <= ACCOUNT_SPACES) 
     { 
      numAccounts++; 
      printAccountDetails(); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, "Sorry, a maximum limit of accounts allowed has been reached." + "\n" + "Limit: " + numAccounts + "/10", "Warning!", JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 

    /** 
    * Method to print the account details - by calling an object from the BankAccount class. 
    */ 

    private void printAccountDetails() 
    { 
     JOptionPane.showMessageDialog(null, newAccount.toString(), "Account Details", JOptionPane.INFORMATION_MESSAGE); 
    } 

    public void listAllAccounts() 
    { 
     String allAccountsString = "List of all accounts: \n"; 

     for(int i = 0; i < ACCOUNT_SPACES; i++) 
     { 
      if (accountArray[i] !=null) 
      { 
       allAccountsString += accountArray[i].toString() + "\n\n" ; 

      } 
      JOptionPane.showMessageDialog(null, allAccountsString); 
     } 
    } 

    public void listAllOpenAccounts() 
    { 

     String allAccountsString = "List of all accounts: \n"; 

     for(int i = 0; i < ACCOUNT_SPACES; i++) 
     { 
      //allAccountsString += accountArray[numAccounts]; 
      if (accountArray[i] !=null && accountStatus == true) 
      { 
       allAccountsString += accountArray[i].toString() + "\n\n" ;`` 
      } 
     } 
     JOptionPane.showMessageDialog(null, allAccountsString); 
    } 
} 
+2

我想,'accountArray [I]'總是空。 – 2015-02-10 12:57:56

+1

Account_spaces的值是什麼,另外爲什麼你不使用accountArray.length,以及這個數組有什麼來源,因爲它在代碼示例中不可見。 – SomeJavaGuy 2015-02-10 12:58:09

+0

您可以嘗試在for循環之前打印出accountsArray [0],以查看是否真的存在隱藏的內容 – Johan 2015-02-10 13:05:16

回答

0

感謝您的幫助。我使用了一個名爲newAccount的變量,而不是數組本身。

的解決方案是:

/** 
* Importing JOptionPane for user GUI. 
*/ 
import javax.swing.JOptionPane; 
import javax.swing.*; 
/** 
* The MyBankController class will control the creation of accounts utilizing the BankAccount class you 
created in Part A of the assignment 
* 
* @author Katarzyna Korzeniec 
* @version 03/02/2015 
*/ 
public class MyBankController 
{ 
    /** 
    * Variables that will be used by this class 
    */ 

    //private BankAccount newAccount; 
    private BankAccount accountArray[] = new BankAccount[2]; 
    int numAccounts = 0; 
    int ACCOUNT_SPACES = 2; 
    //boolean accountStatus = false; 

    /** 
    * Constructor for objects of class MyBankController - to be left empty by requirements. 
    */ 
    public MyBankController() 
    { 
     // 
    } 

    /** 
    * A method to create a new account, accepting user input and allocating memory space. 
    */ 
    public void createAccount(String customerName, int accountNumber) 
    { 


     if(numAccounts +1 <= ACCOUNT_SPACES) 
     { 
      accountArray[numAccounts] = new BankAccount(customerName, accountNumber); 

      printAccountDetails(numAccounts); 
      numAccounts++; 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, "Sorry, a maximum limit of accounts allowed has been reached." + "\n" + "Limit: " + numAccounts + "/10", "Warning!", JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 

    /** 
    * Method to print the account details - by calling an object from the BankAccount class. 
    */ 

    private void printAccountDetails(int value) 
    { 
     JOptionPane.showMessageDialog(null, accountArray[value].toString(), "Account Details", JOptionPane.INFORMATION_MESSAGE); 
    } 

    public void listAllAccounts() 
    { 
     String allAccountsString = "List of all accounts: \n"; 

     for(int i = 0; i < ACCOUNT_SPACES; i++) 
     { 
      if (accountArray[i] !=null) 
      { 
       allAccountsString += accountArray[i].toString() + "\n\n" ; 

      } 

     } 
     JOptionPane.showMessageDialog(null, allAccountsString); 
    } 

    public void listAllOpenAccounts() 
    { 

     String allAccountsString = "List of all accounts: \n"; 

     for(int i = 0; i < ACCOUNT_SPACES; i++) 
     { 
      //allAccountsString += accountArray[numAccounts]; 
      if (accountArray[i] !=null && (accountArray[i].getAccountStatus() !=false)) 
      { 
       allAccountsString += accountArray[i].toString() + "\n\n" ; 
      } 
     } 
     JOptionPane.showMessageDialog(null, allAccountsString); 
    } 
} 
1

只是用於測試目的試試這個,看看會發生什麼:

public void listAllAccounts() 
{ 
    String allAccountsString = "List of all accounts: \n"; 

    if (accountArray.length == 0) { 
     allAccountsString += "the array is empty, there are no accounts\n"; 
    } 

    for(int i = 0; i < ACCOUNT_SPACES; i++) 
    { 
     //allAccountsString += accountArray[numAccounts]; 
     if (accountArray[i] !=null) 
     { 
      allAccountsString += accountArray[i].toString() + "\n\n" ; 
     } else { 
      allAccountsString += "null value here \n\n" ; 
     } 
    } 
    JOptionPane.showMessageDialog(null, allAccountsString); 
1

如果你都不放過一個預定義的字符串,有是以下情況需要考慮:

  1. ACCOUNT_SPACES可能爲0,因此您永遠不會進入for循環。 補救措施:運行循環,直到達到數組長度或使用foreach構造;
  2. accountArray[i] !=null爲false,因此您的數組包含空條目。 補救措施:您的數組必須預填充一些數據。

另外請注意,accountArray[i] !=null不確保ArrayIndexOutOfBoundsException不會被拋出。因此,應該遍歷數組,直到array.length - 1元素爲此不發生。

最後需要注意的是,Swing組件並不考慮換行符(\n),但它們可以包含一些基本的HTML代碼以增強圖形輸出。因此,將\n替換爲<br/>以實現換行似乎是合理的。還請閱讀How to Use HTML in Swing Components指南。