查找ArrayList对象 - 搜索不起作用?

时间:2013-07-19 18:26:10

标签: java arraylist

我正在创建一个可以存入,取款,创建帐户并显示所有余额的银行。

我的createButton方法运行正常 -

    public void createNewAccountButtonPanel(){
    //create button
    createButton = new JButton("Create New Account");
    //Add Listener modeled from InputFrame.Java from GroupProject
    class AddCreateNewListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent CreateNew){
            //account number has to be 4 digits. Balance has to be 100 or more
            if(accountField.getText().trim().length() != 4 || balanceField.getText().trim().length() < 3){
                //not correct input, tell the user to enter the correct input
                System.out.println("Failed to create a Bank Account!");
                textArea.append("Please enter a Account number and a Balance!" + "\n");
            }
            else
            {//read the input
                System.out.println("Creating a Bank Account!");
                Integer accountNumber = Integer.parseInt(accountField.getText());
                Double amount = Double.parseDouble(balanceField.getText());
                getBank().createNew(accountNumber, amount);
                textArea.append("You created " + getBank().accounts.get(getBank().accounts.size()-1) + "   \n");
            }
            }   
    }
    createNew = new AddCreateNewListener();
    createButton.addActionListener(createNew);  
}

这是我的搜索 - 它总是返回null,即使我知道我已经添加了一个帐户......

    public BankAccount search(Integer accountNumber){
    BankAccount found = null;
    for(BankAccount a : accounts){
        if(a.getAccountNumber() == accountNumber) {
            System.out.println("Found the account!");
            found = a;     
        }
        else{
            System.out.println("The Account Number you entered was not found.");
            found = null;
        }
    }       
    return found;
}

我错过了什么?这也导致我的存款和提款按钮现在工作。我的显示所有帐户都运行正常。

编辑:看来我的搜索工作正常,我添加了break。现在问题是如何在gui的文本区域显示余额 - 它总是显示0.0

    public void displayBalancePanel(){
    //create the button
    displayBalanceButton = new JButton("Display The Balance");
    //Add listener modeled from InputFrame.java from GroupProject
    class AddDisplayBalanceListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent DisplayBalance){
            //read the input
            Integer accountNumber = Integer.parseInt(accountField.getText());
            System.out.println("accountNumber to Display Balance for: " + accountNumber);
            getBank().displayBalance(accountNumber, amount);
            textArea.append("The Balance for Account: " + accountNumber + " is " + getAmount() + "\n");
        }
    }
    displayBalance = new AddDisplayBalanceListener();
    displayBalanceButton.addActionListener(displayBalance);
}

新搜索 -

    public BankAccount search(Integer accountNumber){
    BankAccount found = null;
    for(BankAccount a : accounts){
        if(a.getAccountNumber().equals(accountNumber)) {
            System.out.println("Found the account!");
            found = a;     
            System.out.println("a: " + a);
            break;
        }
        else{
            System.out.println("The Account Number you entered was not found.");
            found = null;
        }
    }       
    return found;
}

2 个答案:

答案 0 :(得分:1)

因为它是一个整数(不是int):

a.getAccountNumber().intValue() == accountNumber.intValue();

可能就是你想要的。此外,他们所说的关于打破循环。

答案 1 :(得分:0)

找到该帐户后,您的for循环会越过所需的Account。然后elsefound设置回null

找到该帐户后,只需从if块中将其返回即可。您甚至不需要else阻止。

实际上,根本不需要变量found。只需直接在a块中返回if,如果找不到,则方法中的最后一个语句可以更改为return null;