2017-10-28 49 views
-2

我有一个项目的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); 

}

我的文本文件是 -

bankfile.txt

+0

http://idownvotedbecau.se /没有调试/ – Oleg

+0

你可以显示你的txt文件吗? – icarumbas

+0

请将完整的源代码添加到您的问题。 – Progman

回答

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; 
} 
} 
+0

我无法弄清楚它是由于while循环还是构造函数运行两次(如果是)。 –

+0

尝试把断点符合construcror从文件并运行debbuger读取(IntelijIdea不然)。这将显示它正在呼叫。 –

+0

谢谢@Mykhailo Voloshyn。构造函数被调用两次。 由于这个 - 'BankAccount ba = new BankAccount()' –