2010-05-05 61 views
-1

我需要一些幫助從文本文件中讀取數據到我的ArrayList。第一部分創建並將ArrayList放入文本文件中工作完美。我只需要在「標記」區域的最後一些幫助。如何在Java中將數據從文件讀入數組?

請注意,一些標識符是我的母語。

public class ContAngajat { 
    String username; 
    String password; 
} 

public class CreazaCont { 

// creating the arraylist and putting it into a file 

public static void ang(String args[]) { 

    ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50); 

Scanner diskScanner = new Scanner(in); 

Scanner forn = new Scanner(in); 


int n; 

    out.print("Introduceti numarul de conturi noi care doriti sa le introduceti: "); 
    n=forn.nextInt(); 
    out.println(); 

    try{ 

    FileWriter fw = new FileWriter("ConturiAngajati.txt", true); 


    for(int i=0; i<n; i++){ 
    ContAngajat cont = new ContAngajat(); 

    out.print("Username: "); 
    cont.username=diskScanner.nextLine(); 


    out.print("Password: "); 
    cont.password=diskScanner.nextLine(); 

    angajati.add(cont); 

    fw.write(cont.username + " "); 
    fw.write(cont.password +"|"); 


    } 
    fw.close(); 
    } 
    catch(IOException ex){ 

     System.out.println("Could not write to file"); 

     System.exit(0); 
    } 




for (int i=0; i<n; i++) { 
    out.println("username: " + angajati.get(i).username + " password: " +angajati.get(i).password); 

} 


    } 


// HERE I'M TRING TO GET THE ARRAYLIST OUT OF THE FILE 

public static void RdAng(String args[]) { 

ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50); 
ContAngajat cont = new ContAngajat(); 
int count,i2,i; 

try{ 


    FileReader fr = new FileReader("ConturiAngajati.txt"); 
    BufferedReader br = new BufferedReader(fr); 

    String line = ""; 


    while((line=br.readLine())!=null) { 

    String[] theline=line.split("|"); 
    count=theline.length; 

    for(i=0;i<theline.length;i++) { 

    String[] theword = theline[i].split(" "); 
    }  

    } 

    for(i2=0;i2<count;i2++) { 

    ContAngajat contrd = new ContAngajat(); 

// "ERROR" OVER HERE 

for (int ird=0; ird <theword.length; ird++) { 

cont.username=theword[0]; 
    cont.password=theword[1]; 

// they keep telling me "theword cannot be resolved" whenever i try to run this 

} 
     angajati.add(contrd); 
} 

} 


catch(IOException ex){ 

     System.out.println("Could not read to file"); 

     System.exit(0); 
    } 
} 


} 

編譯錯誤是theword cannot be resolved

+3

你的代碼在美學上看起來很糟糕。嘗試使用代碼格式化程序(大多數IDE隨附)來格式化它。閱讀很痛苦。 – 2010-05-05 22:09:00

+0

是啊,你必須清理這段代碼......這段代碼太難閱讀 – 2010-05-05 22:13:02

+2

這段代碼很糟糕。我認爲在編寫程序之前學習構建代碼是我可以推薦的最好的事情。程序需要結構化爲不同的方法,組件等。通常還有「做事的好方法」和「做壞事的方法」。這段代碼有很多「糟糕的做事方式」。 有些不是你的錯。大學在教java方面很糟糕。其他人只是早期版本的語言和舊書的問題。 試着學習如何構建你的代碼。 – egervari 2010-05-05 22:46:51

回答

2

他們都跟我「theword解決不了」每當我嘗試運行此

這意味着theword未在範圍內聲明。您無法訪問它來調用任何方法。 他們說得對。您需要在更廣泛的範圍內聲明theword,或者將依賴於theword的代碼移入聲明它的範圍。也許你已經在ifwhile區塊內聲明瞭它,並且你正試圖在之外使用它聲明它的區塊。這是行不通的。

無論何時清理代碼以使其更好可讀,都可以給出更詳細的答案。

+0

所以我宣佈theword出來的時候,它的工作。所以這不再是問題了...... :) 現在它說:在cont.username = theword [0]的線程中的異常; 謝謝你的answear! – Lox 2010-05-05 22:33:39

+1

不客氣。如果您遇到新問題,請提出一個新問題。不要忘記正確地格式化代碼(例如Eclipse中的Ctrl + Shift + F),幷包含整個異常的堆棧跟蹤,並將代碼行指向代碼。 – BalusC 2010-05-05 22:35:52