数组未显示(Java)

时间:2015-02-10 12:56:06

标签: java arrays joptionpane

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

  

"此方法使用循环列出数组中包含的所有帐户,将每个帐户详细信息添加到字符串中,然后以下面屏幕截图中指定的格式输出到屏幕。通过在将其详细信息添加到输出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);
    }
}

3 个答案:

答案 0 :(得分: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)

如果您不使用预定义的字符串,则需要考虑以下情况:

  1. ACCOUNT_SPACES可能为0,因此您永远不会进入for循环。 Remedy :运行循环直到达到数组长度或改为使用foreach结构;
  2. accountArray[i] !=null为false,因此您的数组包含空条目。 补救措施:您的数组必须预先填充一些数据。
  3. 另请注意,accountArray[i] !=null无法确保不会抛出ArrayIndexOutOfBoundsException。因此,应该遍历数组,直到array.length - 1元素为止不会发生。

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

答案 2 :(得分: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);
    }
}
相关问题