gpt4 book ai didi

java - 搜索保存在文件中的 ArrayList 中的数据并将其打印在文本字段中

转载 作者:行者123 更新时间:2023-12-01 14:31:44 25 4
gpt4 key购买 nike

我有这个按钮,可以通过 ArrayList 中的文本字段保存输入数据并将其保存到文件中。

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);
}
}

我还有一个 Withdraw_AccountName 和 Withdraw_AccountNumber 的文本字段,我如何在数组列表中搜索帐号?当Withdraw_AccountNumber与数组列表中的帐号匹配时,也会在Withdraw_AccountName中显示帐户名称。请帮忙。

最佳答案

您需要遍历 BankAccounts 以找到具有您所需帐号的银行帐户。

for(BankAccount account: list) {
if (account.getAccountNo().equals(accountNumberToSearchFor)){
// found ya! set the correct text field
break;
}
}

或者,您可以将 BankAccounts 存储在 Map 中,您可以在其中制作帐号到银行帐户的映射;或者,您也可以创建一个自定义比较器并使用现在按帐号排序的 Java 集合。

关于java - 搜索保存在文件中的 ArrayList 中的数据并将其打印在文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com