2012-11-14 22 views
0

我想讀取一個csv文件到一個ArrayList或一個String [] []數組。在這裏,我試圖將它讀入一個列表,然後使用一個標記器將這個列表形成一個數組。 csv文件有7列(A - G)和961行(1-961)。我的for循環的二維數組不斷返回一個空指針,但我認爲這應該是工作..讀取沒有第三方庫的CSV文件

public class FoodFacts 
{ 
    private static BufferedReader textIn; 
    private static BufferedReader foodFacts; 
      static int numberOfLines = 0; 
      static String [][] foodArray; 
    public static String aFact; 
    static int NUM_COL = 7; 
    static int NUM_ROW = 961; 
    // Make a random number to pull a line 
    static Random r = new Random(); 

    public static void main(String[] args) 
    { 
     try 
     { 
      textIn = new BufferedReader(new InputStreamReader(System.in)); 
      foodFacts= new BufferedReader(new FileReader("foodfacts.csv")); 
      Scanner factFile = new Scanner(foodFacts); 
      List<String> facts = new ArrayList<String>(); 

      String fact; 
      System.out.println("Please type in the food you wish to know about."); 
      String request = textIn.readLine(); 
      while (factFile.hasNextLine()){ 
       fact = factFile.nextLine(); 
       StringTokenizer st2 = new StringTokenizer(fact, ","); 
       //facts.add(fact); 
       numberOfLines++; 
       while (st2.hasMoreElements()){ 
        for (int j = 0; j < NUM_COL ; j++) { 
         for (int i = 0; i < NUM_ROW ; i++){ 
          foodArray [j][i]= st2.nextToken(); //NULL POINTER HERE 
          System.out.println(foodArray[j][i]); 
         } 
        } 
       } 
      } 
     } 
     catch (IOException e) 
     { 
      System.out.println ("Error, problem reading text file!"); 
      e.printStackTrace(); 
     } 
    } 
} 
+0

您想要閱讀哪種叫做「CSV」的幾種格式? – Raedwald

+0

它對Excel的?逗號分隔值 –

+0

你有*沒有第三方庫嗎? –

回答

3

使用它之前初始化您foodArrayfoodArray = new String[NUM_ROW][NUM_COL];

此外,您不需要內部for循環,因爲您一次只讀一行。

使用numberOfLines爲行:

 while (factFile.hasNextLine() && numberOfLines < NUM_ROW){ 
       fact = input.nextLine(); 
       StringTokenizer st2 = new StringTokenizer(fact, ",") ; 
       //facts.add(fact); 
       while (st2.hasMoreElements()){ 
        for (int j = 0; j < NUM_COL ; j++) { 
        foodArray [numberOfLines][j]= st2.nextToken(); 
        System.out.println(foodArray[numberOfLines][i]); 
       } 
       } 
       numberOfLines++; 
      } 

另外,我認爲你可以使用split讓所有列,因爲一旦如

 while (factFile.hasNextLine() && numberOfLines < NUM_ROW){ 
      fact = input.nextLine(); 
      foodArray [numberOfLines++] = fact.split(","); 
     } 

一個問題:是否有聲明所有變量爲靜態類變量任何具體的目的是什麼?它們中的大多數適合作爲方法內的局部變量,例如numberOfLines

+0

我一直扔在一起,當我得到我的數組保持Excel表單元格我會清理它很多 –

+0

@FredV:好的。這對你有用嗎?另外,它有沒有理由不使用'split()'方法? –

+0

@FredV:我不確定你是否已經注意到,我更新while條件爲while(factFile.hasNextLine()&& numberOfLines

0

您可以使用此String [][] foodArray = csvreadString(filename);方法。它實際上讀取文件兩次,但我不知道如何在不讀取數據的情況下獲取csv維度(您需要維度來初始化數組),並且與我嘗試的其他方法相比,這非常快。

static public class PairInt { 

     int rows = 0; 
     int columns = 0; 
    } 
    static PairInt getCsvSize(String filename) throws Throwable { 
     PairInt csvSize = new PairInt(); 
     BufferedReader reader = new BufferedReader(new FileReader(new File(filename))); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      if (csvSize.columns == 0) { 
       csvSize.columns = line.split(",").length; 
      } 
      csvSize.rows++; 
     } 
     reader.close(); 
     return csvSize; 
    } 
    static String[][] csvreadString(String filename) throws Throwable { 
     PairInt csvSize = getCsvSize(filename); 
     String[][] data = new String[csvSize.rows][csvSize.columns]; 
     BufferedReader reader = new BufferedReader(new FileReader(new File(filename))); 
     for (int i = 0; i < csvSize.rows; i++) { 
      data[i] = reader.readLine().split(","); 
     } 
     return data; 
    }