2015-02-10 31 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指南。