2016-11-30 55 views
-2

我目前被困在類項目的這一部分...我需要從文本文件創建一個ArrayList;文本文件具有需要在JComboBox中填充的帳號。這是我迄今爲止...只有第一個帳號填充缺失,其餘不知道我的錯誤是什麼用不同類的ArrayList填充JComboBox

// AccountUtility class that reads file and creates ArrayList named test 

public class AccountUtility { 

    ArrayList<String> test = new ArrayList<String>(); 
    String[] number; 
    String columns[], accountNumber, customerName, openDate, balance; 
    int size; 


public AccountUtility(){ 


    BufferedReader in = null; 
    try{ // assume products.txt already exists 
    in = new BufferedReader(new FileReader("accounts.txt")); 
    String line = in.readLine(); 
    while(line != null) { 
    columns = line.split("<>"); 
    accountNumber = columns[0]; 
    customerName = columns[1]; 
    openDate = columns[2]; 
    balance = columns[3]; 

        line = in.readLine(); 
      } 
      in.close(); 
    } 
    catch(IOException ioe) 
    { 
      System.out.println(ioe); 
    } 
} 

public ArrayList <String> getAccountNumbers(){ 

    ArrayList <String> test = new ArrayList<String>(); 
    test.add(accountNumber); 

    return test; 




//class with JComboBox (GUI) 

public class BankAccountApp extends javax.swing.JFrame { 


    public BankAccountApp() { 

     initComponents(); 
     setLocationRelativeTo(null); 

     AccountUtility gc = new AccountUtility(); 

     for(String numbers : gc.getAccountNumbers()){ 
     accountNumberComboBox.addItem(numbers); 
     } 
    } 
+0

不要在構造函數中執行操作! –

回答

0

我本來以爲你想要的是

while(line != null) { 
    columns = line.split("<>"); 
    accountNumber = columns[0]; 
    test.add(accountNumber); 
    .... 
} 

public ArrayList getAccountNumbers(){ 
    return test; 
} 
+0

謝謝你的工作! –

+0

如果這個答案有用,請考慮upvote和/或接受。 –