0

我正在尝试将 csv 文件读入 ArrayList 或 String [][] 数组。在此,我试图将其读入列表,然后使用标记器将列表形成为数组。csv 文件有 7 列 (A - G) 和 961 行 (1-961)。我的二维数组的 for 循环不断返回一个空指针,但我认为它应该可以工作..

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();
        }
     }
 }
4

2 回答 2

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

于 2012-11-14T17:44:10.867 回答
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;
    }
于 2012-11-14T17:57:14.107 回答