Reputation: 1
i have this button that save input data via textfield in an ArrayLists and save it to a file.
private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) {
BankAccount account = new BankAccount();
ButtonGroup bg = new ButtonGroup();
bg.add(rad_savings);
bg.add(rad_checking);
account.setAccountName(txt_accountname.getText());
account.setAccountNo(txt_accountnumber.getText());
list.add(account);
String fileName = "bank.txt";
FileWriter file = null;
try {
file = new FileWriter(fileName,true);
PrintWriter pw = new PrintWriter(file);
for(BankAccount str: list) {
pw.println(str);
}
pw.flush();
pw.println("\n");
} catch(FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "Can't create your account!");
} catch (IOException ex) {
Logger.getLogger(JFrameNewAccount.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
file.close();
} catch (IOException ex) {
Logger.getLogger(JFrameNewAccount.class.getName()).log(Level.SEVERE, null, ex);
}
}
I also have a textfield for Withdraw_AccountName and Withdraw_AccountNumber, how can i search the account number inside the arraylists? When Withdraw_AccountNumber match with the account number in the arraylists, it will also display the account name in the Withdraw_AccountName. Please help.
Upvotes: 0
Views: 327
Reputation: 7507
you will need to iterate over the BankAccounts to find the one that has the account number you need.
for(BankAccount account: list) {
if (account.getAccountNo().equals(accountNumberToSearchFor)){
// found ya! set the correct text field
break;
}
}
Alternatively you could store the BankAccounts in a Map, where you make a map of account numbers to bank accounts; or, you could also make a custom comparator and use a Java Collection that is now sorted by the account number.
Upvotes: 1