Arraylist从文件输出中读取两次

时间:2017-10-28 08:46:00

标签: java

我有一个项目的ArrayList - 从.txt文件读取的名称,帐号和余额。为什么输出两次? (我的文件中只有4行包含姓名;帐号;余额)
我只想要蓝线以上的东西。为什么要显示两次?

这是我从文件中检索并打印的课程

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

}

我的文字文件是 -

bankfile.txt

1 个答案:

答案 0 :(得分:0)

抱歉,我没有收到错误。希望这会对你有所帮助。我认为问题是你两次运行这个构造函数。另外,我运行你的代码。它的确有效。

我的 Main.java

public class Main {

public static void main(String[] args) {
    // write your code
    new BankAccount();
}
}

我的档案 bankfile.txt

lala;0;0
coca;0;1
bola;1;1

我的 BankAccount.java

import java.io.*;
import java.util.ArrayList;

public class BankAccount {

private  String name;
private  long accountNumber;
private  float 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);
        }
        for(BankAccount c: customer_list) System.out.println(c.getName()+" "+c.get_Balance());
    }catch (FileNotFoundException e) {e.printStackTrace();
    } catch (IOException e) { e.printStackTrace();
    } finally {
        if (in != null) {
            try{ in.close();
            } catch(Exception e){e.printStackTrace();}
        }
    }
}
 public BankAccount(String name, Long accountNumber, float balance){
    this.name = name;
    this.balance = balance;
    this.accountNumber = accountNumber;
 }

public String getName() {
    return name;
}

public long getAccountNumber() {
    return accountNumber;
}

public float get_Balance() {
    return balance;
}
}