我有一个项目的ArrayList - 姓名,账号和平衡从.txt文件中读取。为什么输出两次? (我的文件中只有4行包含名称;帐号;余额)
我希望只有蓝线以上的东西。为什么显示两次?ArrayList中从文件输出读取两次
这是我的同班同学,从文件和打印
class BankAccount{ //core banking facilities
PrintStream pw=System.out;
protected String name;
protected long accountNumber;
protected float balance=0;
public String choiceList="\n1.AddAccount\n2.Withdraw\n3.Deposit\n4.MyAccount";
public BankAccount(String name,long accountNumber,float balance){
this.accountNumber=accountNumber;
this.name=name;
this.balance = balance;
}
public String getName(){
return this.name;
}
public long get_Account_no(){
return this.accountNumber;
}
public float get_Balance(){
return this.balance;
}
public BankAccount(){//loads from file to arraylist
BufferedReader in = null;
ArrayList <BankAccount> customer_list=new ArrayList<BankAccount>();
try {
in = new BufferedReader(new FileReader("bankfile.txt"));
String str;
while ((str = in.readLine()) != null) {
String[] temp_list=str.split(";");
accountNumber=Long.parseLong(temp_list[1]);
balance=Float.parseFloat(temp_list[2]);
BankAccount customer = new BankAccount(temp_list[0],accountNumber,balance);
customer_list.add(customer);
}
}catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} finally {
if (in != null) {
try{ in.close();
} catch(Exception e){e.printStackTrace();}
}
}
for(BankAccount c: customer_list) pw.println(c.getName()+" "+c.get_Balance()+"\n");
}
} 检索和我的主要是 -
class banker {
public static void main(String args[]){
additional_functionality af=new additional_functionality();
BankAccount ba=new BankAccount();
ba.pw.println("This is.. \n \t\tTHE BANK\nplease wait...");
String ch=JOptionPane.showInputDialog(ba.choiceList);
Integer choice=Integer.parseInt(ch);
switch(choice){
case 1: af.addAcount();
break;
case 4: //af.findAccount();
break;
default: JOptionPane.showMessageDialog(null,"Wrong Choice!","ERR",JOptionPane.ERROR_MESSAGE);
}System.exit(0);
}
我的文本文件是 -
http://idownvotedbecau.se /没有调试/ – Oleg
你可以显示你的txt文件吗? – icarumbas
请将完整的源代码添加到您的问题。 – Progman