数组未显示 (Java)

Array is not displaying (Java)

我的一种作业方法有问题。这是此方法的要求:

"This method uses a loop to list all accounts contained in the Array, adding each account details to a String, before outputting to screen in the format specified in the screenshot below. Ensure that there are no out of bounds exceptions by checking if each array slot has an account object before adding its details to the output 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";

有什么想法吗?

这是整个代码 class:

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

只是为了测试目的试试这个,看看会发生什么:

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. ACCOUNT_SPACES 可能为 0,因此您永远不会进入 for 循环。 补救措施:运行循环直到达到数组长度或改用foreach结构;
  2. accountArray[i] !=null 为假,因此您的数组包含空条目。 补救措施:您的数组必须预先填充一些数据。

另外请注意,accountArray[i] !=null 并不能确保 ArrayIndexOutOfBoundsException 不会被抛出。因此,应该遍历数组直到 array.length - 1 个元素,以免发生这种情况。

最后要注意的是,Swing 组件不支持换行符 (\n),但它们可以包含一些基本的 HTML 代码以增强图形输出。因此,将 \n 替换为 <br/> 以实现换行似乎是合理的。另请阅读 How to Use HTML in Swing Components 指南。

感谢您的帮助。我使用了一个名为 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);
    }
}