How do I iterate through a .txt file and store the result in a 2d arrayList

Viewed 53

The following is a snippet of a txt file

Patrick Joseph
1254367
cmt101
Introduction to Computer Science
1
Computer Science
Spring
2020
B+
3
cmt102
Computer Networks
2
Computer Science
Spring
2020
A
3

Every 8 lines minus the first 2 represents an attribute of a course in a college. I am trying to sort the data into two arrayLists, course and courses. course will be an arrayList containing attributes for each course while courses will contain all the courses such that the data in courses should look something like this:

[0][0] cmt101
[0][1] Introduction to Computer Science
[0][2] 1
[0][3] Computer Science
[0][4] Spring 
[0][5] 2020
[0][6] B+
[0][7] 3

[1][0] cmt102
[1][1] Computer Networks
[1][2] 2
[1][3] Computer Science
[1][4] Spring 
[1][5] 2020
[1][6] A
[1][7] 3

and so on. The program should be able to iterate regardless of how many courses are added to the file while skipping the first two lines

    public static ArrayList<ArrayList<String>> courses;
    public static ArrayList<String> course;  
    
    public static void getCourses(){
                try{
                    course = new ArrayList<>();
                    courses = new ArrayList<>();
                    String fileName = "C:\\Users\\myComputer\\Desktop\\file.txt";
                    List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
                    for (int i = 2; i < lines.size(); i += 8){
                        for (int j = 2; j <= 10; j++){
                            course.add(lines.get(j));
                        }
                        courses.add(course);
                    }
                }
                catch(IOException io){
                    io.printStackTrace(System.out);
                }
            }
// courses is printed in another method

Any help will be appreciated

1 Answers

The problem is that you use the same reference to the inner list which is filled with each iteration over and over. You need to create a new instance between the inner and outer loop and limit its scope. This will assure that a new instance will always be created and inserted into the outer list.

I also find no reason for instantiating these lists inside the method. Moreover, ArrayList<String> course; has a too broad scope and breaks the logic you intend to use. Lastly, program against the interface: List<String> list = new ArrayList<>();

public static List<List<String>> courses = new ArrayList<>();
 
public static void getCourses() {
    try {
        String fileName = "C:\\Users\\myComputer\\Desktop\\file.txt";
        Charset charset = Charset.defaultCharset();
        List<String> lines = Files.readAllLines(Paths.get(fileName), charset);

        for (int i = 2; i < lines.size(); i += 8) {
            List<String> course = new ArrayList<>();
            for (int j = 2; j <= 10; j++) {
                course.add(lines.get(j));
            }
            courses.add(course);
        }
    } catch(IOException io) {
        io.printStackTrace(System.out);
    }
}
Related