Search code examples
javaarraylisttext-fileslogic-error

How to read a text file into an array list of objects in Java


I'm currently working on a project and I'm running into a couple of issues. This project involves working with 2 classes, Subject and TestSubject. Basically, I need my program (in TestSubject class) to read details (subject code and subject name) from a text file and create subject objects using this information, then add those to an array list. The text file looks like this:

ITC105: Communication and Information Management
ITC106: Programming Principles
ITC114: Introduction to Database Systems
ITC161: Computer Systems
ITC204: Human Computer Interaction
ITC205: Professional Programming Practice

the first part is the subject code i.e. ITC105 and the second part is the name (Communication and Information Management)

I have created the subject object with the code and name as strings with getters and setters to allow access (in the subject class):

private static String subjectCode;
private static String subjectName;

public Subject(String newSubjectCode, String newSubjectName) {
    newSubjectCode = subjectCode;
    newSubjectName = subjectName;
}

public String getSubjectCode() {
    return subjectCode;
}
public String getSubjectName() {
    return subjectName;
}

public void setSubjectCode(String newSubjectCode) {
    subjectCode= newSubjectCode; 
}

public void setSubjectName(String newSubjectName) {
    subjectName = newSubjectName; 
}

The code I have so far for reading the file and creating the array list is:

public class TestSubject {
   @SuppressWarnings({ "null", "resource" })
   public static void main(String[] args) throws IOException {
    
    File subjectFile = new File ("A:\\Assessment 3 Task 1\\src\\subjects.txt");
    Scanner scanFile = new Scanner(subjectFile);
    
    System.out.println("The current subjects are as follows: ");
    System.out.println(" ");

    while (scanFile.hasNextLine()) {
        System.out.println(scanFile.nextLine());
    }
    
    //This array will store the list of subject objects. 
    ArrayList <Object> subjectList = new ArrayList <>();
    
    //Subjects split into code and name and added to a new subject object.
    String [] token = new String[3];
    
    while (scanFile.hasNextLine()) {
        token = scanFile.nextLine().split(": ");
        String code = token [0] + ": ";
        String name = token [1];
        
        Subject addSubjects = new Subject (code, name);
        
        //Each subject is then added to the subject list array list. 
        subjectList.add(addSubjects);
    }
    
    //Check if the array list is being filled by printing it to the console.
    System.out.println(subjectList.toString());

This code isn't working, the array list is just printing as blank. I have tried doing this several ways including a buffered reader but I can't get it to work so far. The next section of code allows a user to enter a subject code and name, which is then added to the array list as well. That section of code works perfectly, I'm just stuck on the above part. Any advice on how to fix it to make it work would be amazing.

Another small thing:

 File subjectFile = new File ("A:\\Assessment 3 Task 1\\src\\subjects.txt"); //this file path
 Scanner scanFile = new Scanner(subjectFile);

I'd like to know how I can change the file path so that it will still work if the folder is moved or the files are opened on another computer. The .txt file is in the source folder with the java files. I have tried:

 File subjectFile = new File ("subjects.txt");

But that doesn't work and just throws errors.


Solution

  • That is because you have already read through the file

    while (scanFile.hasNextLine()) {
        System.out.println(scanFile.nextLine());
    }
    

    The contents are exhausted. So when you do

    while (scanFile.hasNextLine()) {
        token = scanFile.nextLine().split(": ");
    

    there is no data left.

    Remove the first loop or re-open the file.

    Or as @UsagiMiyamoto mentions

    Or read the line to a String variable, print it, then split it... All in one loop.