【发布时间】:2025-11-22 10:05:01
【问题描述】:
您能否告知我下面的代码缺少什么?我的代码正在运行,但它没有写入文件。通过文本字段输入 where。以下是我所拥有的。
public class JFrameNewAccount extends javax.swing.JFrame {
ArrayList<BankAccount> list = new ArrayList<BankAccount>();
BankAccount account = new BankAccount();
public JFrameNewAccount() {
initComponents();
}
private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) {
account.setAccountName(txt_accountname.getText());
account.setAccountNo(txt_accountnumber.getText());
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);
}
}
请指教。
【问题讨论】:
-
我没有看到代码,它将
BankAccount对象添加到list.. -
你能给我举个例子吗?
-
您的
list似乎是空的。将 backAccount 添加到列表中。list.add(backAccount); -
它打印错误的输出:它在文件上打印:finalassignment.BankAccount@b80017 当我尝试使用 list.add(BankAccount); 时它给了我一个错误;
account.setAccountName(txt_accountname.getText()); account.setAccountNo(txt_accountnumber.getText()); list.add(account); -
你能用上面的评论更新问题吗?
标签: java swing arraylist textfield