Search code examples
c#listcsvstorebufferedreader

Using Lists to store data from a BufferedReader file


I've been trying to use lists to store data from a .csv file, but since I'm new to using lists, I can't seem to be able to get it to work. I've searched many tutorials but none of them are working for me. I must be missing something. Here's what 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;

        ArrayList<String[]> arrayOfNumbers = new ArrayList<String[]>();

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

            while (st.hasMoreTokens()) {
                //get next token and store it in the array
                // UNSURE WHAT TO PUT HERE
            }
            row++;
        }

Then I want to store the data in that list into a multi-dimensional array called String[][] numbers. Any ideas of what I should do?


Solution

  • before you can initialize your multidim array of Strings you need to know the dimension of it which is analog to your maximum column-size (in case the lines do not have the same amount of 'tokens') and the number of rows which is number of lines you've got.

        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;
    
        ArrayList<StringTokenizer> arrayOfNumbers = new ArrayList<StringTokenizer>();
    
        //read each line of text file
        while((line = bufRdr.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line,",");
            arrayOfNumbers.add(st);
    
            int actColCount = st.countTokens();
    
            if (col < actColCount) {
                 col = actColCount;
            }
        }
    
        row = arrayOfNumbers.size();
    

    now you can iterate over the stored lines and put them into your array. maybe there occure some ArrayOutOfBound Exceptions, didn't went into detail too much. but that should do.

        String[][] numbers = new String[row][col];
    
        for (int i=0; i<row; i++) {
    
            StringTokenizer st = arrayOfNumbers.get(i);
    
            int j = 0;
            while(st.hasMoreElements()) {
                 numbers[i][j] = st.nextElement();
                 j++;
            }
        }
    

    please be careful with those fields in the array which are not have a value because there were not enough elements in that line to fill every column/field in that row. this fields return a null.