[英]Find an ArrayList Object - Search not working?
我正在創建一個可以存入,提取,創建帳戶並顯示所有余額的銀行。
我的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;
}
由於它是整數(不是整數):
a.getAccountNumber().intValue() == accountNumber.intValue();
可能就是您想要的。 另外,他們所說的打破循環。
找到帳戶后,您的for
循環將繼續經過所需的Account
。 然后, else
組found
回null
。
找到該帳戶后,只需在if
塊中從那里將其返回即可。 您甚至不需要else
塊。
實際上,您根本不需要found
該變量。 只需直接在if
塊中返回a
,該方法中的最后一條語句可以更改為return null;
如果找不到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.