Java 搜索保存在文件中的ArrayList中的数据,并在文本字段中打印出来
我有一个按钮,通过ArrayList中的textfield保存输入数据,并将其保存到文件中Java 搜索保存在文件中的ArrayList中的数据,并在文本字段中打印出来,java,swing,arraylist,textfield,Java,Swing,Arraylist,Textfield,我有一个按钮,通过ArrayList中的textfield保存输入数据,并将其保存到文件中 private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) { BankAccount account = new BankAccount(); ButtonGroup bg = new ButtonGroup(); bg.
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);
}
}
我还有一个用于draw\u AccountName和draw\u AccountNumber的文本字段,如何在ArrayList中搜索帐号?当Draw_AccountNumber与ArrayList中的帐号匹配时,它还会在Draw_AccountName中显示帐号名称。请提供帮助。您需要在银行账户上进行迭代,以找到具有所需账号的银行账户
for(BankAccount account: list) {
if (account.getAccountNo().equals(accountNumberToSearchFor)){
// found ya! set the correct text field
break;
}
}
或者,您可以将银行账户存储在地图中,在地图上绘制银行账户的账号地图;或者,您也可以制作一个自定义比较器,并使用现在按帐号排序的Java集合。我希望它们存储为
int
s;)你好,你能帮我找到什么吗?我还想显示accountnameprivate void txt\u wdaccountnumberActionPerformed(java.awt.event.ActionEvent evt){for(BankAccount:list){if(account.getAccountNo()==txt\u wdaccountnumber.getText()){//获取accontname并将其显示在txt_wdaccountname}中,或者{JOptionPane.showMessageDialog(这是“找不到记录!”);}}//要在此处添加处理代码:}
我尝试了以下操作,但没有任何结果。private void txt\u wdaccountnumber执行(java.awt.event.ActionEvent evt){for(BankAccount:list){if(account.getAccountNo().equals(txt\u wdaccountnumber.getText()){txt_wdaccountname.setText(account.getAccountName());txt_wdaccountname}else{JOptionPane.showMessageDialog(这是“找不到记录!”)
什么意思是什么?帐号是文本字段还是原语?帐号是文本字段。我尝试将代码放在txt_wdaccountnumber的操作中。当用户在txt_wdaccountnumber中输入一个数字时,它会在arraylist中搜索帐号,找到时,会搜索与该帐号关联的帐号名称数字也将显示在txt_wdaccountname中,但似乎我遗漏了什么或没有工作?