开发者

Using BufferedReader to count rows in a multi-dimensional array

开发者 https://www.devze.com 2023-03-26 10:44 出处:网络
I\'m using BufferedReader to read a .csv file.I have no problem reading the file and extracting the data.However, the problem that I do have is that I have to hard-code my array declaration.For exampl

I'm using BufferedReader to read a .csv file. I have no problem reading the file and extracting the data. However, the problem that I do have is that I have to hard-code my array declaration. For example:

        String[][] numbers=new String[5258][16];

The .csv file I was using had 5258 rows and 16 columns. I'd like to be able to do something like this though:

        String[][] numbers=new String[rowsInFile][16];

In other words, I want the variable 'rowsInFile' to be equivalent to the amount of rows in the file (I don't want to count the columns, because every .csv file I will be running through this program has 16 columns).

Here's the code I have so far:

        int row = 0;
        int col = 0;

        String fileInput = JOptionPane.showInputDialog(null, 
                "Please enter the path of the CSV file to read:");

        File file = new File(fileInput);

        BufferedReader bufRdr;
        bufRdr = new BufferedReader(new FileReader(file));
        String line = null;

        //get rows in the file
        int rowsInFile = 0;
        while(bufRdr.readLine() != null) {
            rowsInFile++;
            row++;
        }
        String[][] numbers=new String[rowsInFile][16];

        //read each line of text file
        row = 0;
        while((line = bufRdr.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line,",");
            col=0;

            while (st.hasMoreTokens开发者_如何转开发()) {
                //get next token and store it in the array
                numbers[row][col] = st.nextToken();
                col++;
            }
            row++;
        }

However, I'm getting a null pointer exception. Any ideas of what I should do?

P.S. Yes, this code is surrounded by a try/catch statement.


The problem is, once you go through the BufferedReader once, you can't go back through it again. In other words, you have to use a new BufferedReader.

bufRdr = new BufferedReader(new FileReader(file));
row = 0;
while((line = bufRdr.readLine()) != null) {

Alternatively, you could use a dynamic array structure like an ArrayList<String[]> or a LinkedList<String[]> to store the rows.

LinkedList<String[]> numbers = new LinkedList<String[]>();

while( (line = bufRdr.readLine()) != null ) {
    numbers.add(line.split(","));
}

Then instead of doing numbers[i][j], you use numbers.get(i)[j].


Instead of an array use something dynamic like a List. For example:

List<String[]> data = new ArrayList<String[]>();

Also using String's split() method will simplify the loading of the row.


Your problem is that BufferedReaders work by reading until the end of a file, and then they get stuck there. Your code requires reading through the file twice, but because you already reached an EOF, the BufferedReader is stuck returning null. I tend to solve this by stuffing the lines into an ArrayList, and using the size() method to get the number of lines. The source code looks something like this:

    int rowsInFile=0;
    ArrayList<String> lines = new ArrayList<String>();
    String tmp = "";
    while(tmp=bugRdr.readLine())
    {
        lines.add(tmp);
    }

    rowsInFile = lines.size();
    String[][] numbers = new String[rowsInFile][16];

    int row = 0;
    for(String line : lines)
    {
        StringTokenizer st = new StringTokenizer(line,",");
        col=0;

        while (st.hasMoreTokens()) {
        //get next token and store it in the array
            numbers[row][col] = st.nextToken();
            col++;
        }
        row++;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消