Read the contents of a .txt file into a 2D string ARRAYLIST

Hope you can help. Still trying to get my head round java!

I have the contents of a text file I want to store (and use later) into a 2D arraylist. The text file example would look like this (an indeterminate number of entries on an indeterminate number of lines):

dog,cat,monkey,elephant
chip,sausage,beans,mouse,ant
anything,whatever,some object,dunno,every line has a different,number of values

I want to be able to print out the text file just as it stands but with the items neatly stored in my 2D arraylist (biD)
e.g.:

dog|cat|monkey|elephant|
chip|sausage|beans|mouse|ant|
anything|whatever|some object|dunno|every line has a different|number of values|

But when I run the program below I get:

col is 15
dog|cat|monkey|elephant|chip|sausage|beans|mouse|ant|anything|whatever|some object|dunno|every line has a different|number of values|
dog|cat|monkey|elephant|chip|sausage|beans|mouse|ant|anything|whatever|some object|dunno|every line has a different|number of values|
dog|cat|monkey|elephant|chip|sausage|beans|mouse|ant|anything|whatever|some object|dunno|every line has a different|number of values|

i.e. each line in the txt file (there are 3 in this case) seems to produce an entry in the biD structure that contains ALL of the lines in the txt file. Really, each printed line should contain only one lines’ items from the txt file.

Here is the code:

import java.io.File;  
import java.io.FileNotFoundException;  
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.Scanner;  

public class testData {

    private static Scanner file;
    static int rows;

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<ArrayList<String>> biD = new ArrayList<ArrayList<String>>();
        file = new Scanner(new File("C:/tmp/text.txt"));
        ArrayList<String> line = new ArrayList<String>();

        while (file.hasNextLine()) {
            final String nextLine = file.nextLine();
            final String[] items = nextLine.split(",");

            for (int i = 0; i < items.length; i++) {
                line.add(items[i]);
            }

            biD.add(line);
            rows++;
            Arrays.fill(items, null); // to clear out the 'items' array
        }

        int col = biD.get(0).size();
        System.out.println("col is " + col);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < biD.get(i).size(); j++) {
                System.out.print(biD.get(i).get(j) + "|");

            }
            System.out.println();
        }

    }

}

So basically my biD structure seems to be storing too much info.

So a walkthough (I thought) would be:

Read a line from the file
store in a string (nextLine)
split the string and store the items in a string array (items)
Add the items to an arraylist (line)
add that arraylist to the arraylist biD.
(which is where I’m going wrong I think. Probably not understanding how arraylists work properly I think!)

Any ideas, Anyone?

Thanks.

Answer

The line variable is declared once and is written to every time you get a new line. The declaration statement must be moved inside the while loop, so a new one is created to be written for every new line.

public static void main(String[] args) throws FileNotFoundException {
    ArrayList<ArrayList<String>> biD = new ArrayList<ArrayList<String>>();
    file = new Scanner(new File("C:/tmp/text.txt"));
    //ArrayList<String> line = new ArrayList<String>(); <-- Move down to 

    while (file.hasNextLine()) {

        ArrayList<String> line = new ArrayList<String>(); <-- Down here      
        final String nextLine = file.nextLine();
        final String[] items = nextLine.split(",");

        for (int i = 0; i < items.length; i++) {
            line.add(items[i]);
        }

        biD.add(line);
        rows++;
        Arrays.fill(items, null); // to clear out the 'items' array
    }...

Leave a Reply

Your email address will not be published. Required fields are marked *