2011-05-06 76 views
0

我有以下代碼,其中.csv文件被逐行讀取並存儲在String [] nextline中,其中nextline [i]由相應的解析文件的內容。 我沒有問題,在讀取.csv文件,但我確實有它們存儲在另一個字符串[],因爲的System.out.println()給了我下面的結果,對於所有行:將值String []傳遞給String []值

Nextline[1]=Jan 4, 2011 18:33:15.988422000/predata=null/Nextline[4]=54/size:0 

正如你所看到的,Strin []具有空值,爲什麼內容不被傳遞? 在此先感謝

public class Amostra { 
    int[] id = new int[20000]; 
    String[] predata = new String[20000]; 
    int[] size = new int[20000]; 

    Amostra(String namefile) throws FileNotFoundException, IOException { 
     Csv2Array(namefile); 
    } 

    private void Csv2Array(String newfile) throws FileNotFoundException, IOException 
    { 
     String[] nextline; 
     int j = 0; 
     int i = 0; 
     CSVReader reader = new CSVReader(new FileReader(newfile), ';', '\'', 1); 
     while((nextline = reader.readNext()) != null){ 
      predata[i] = nextline[1]; 
      size[i] = Integer.parseInt(nextline[4]); 
      id[i] = i; 
      i++; 
      System.out.println("Nextline[1]=" + nextline[1] + 
        "/predata=" + predata[i] + 
        "/Nextline[4]=" + nextline[4] + 
        "/size:" + size[i] + "\n"); 
     } 
    } 
} 
+0

看到重新格式化的代碼後移動i++行。閱讀起來不容易嗎?如果你使用一致的格式,你會發現更容易發現錯誤。 – 2011-05-06 09:59:35

回答

6

i++語句是System.out之前位於這樣你實際上是打印下一行(尚未解析)。您應該在System.out

+0

...我是怎麼想的?我嘗試了很多東西......我需要睡覺。 Thx;) – Jay 2011-05-06 09:57:12

+0

+1 - 就是這樣!甚至不需要查看'CSVReader' API。 – 2011-05-06 09:58:09

相關問題