Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:08:08+00:00 2026-05-24T23:08:08+00:00

I’m reading in data from different txt files to make objects and then into

  • 0

I’m reading in data from different txt files to make objects and then into arraylists of the objects.

just as an example I have a teacher class with 3 or 4 varibles

public class Teacher {



        public static String teacherNo;
        public static String firstName;
        public static String lastName;
        public static Courses coursestought; 

and a class Courses

public class Course {



        public static String cCode;
        public static String cName;

the constructor for the teacher class is like this

// constructor 
        public Teacher(String teacher_No,String first_Name,String last_Name, 
                Course coursesTought,)

I have all the right getters and setters(I think)

I read from the text file like this.

BufferedReader inFile = new BufferedReader (new FileReader 
           ("pathtofile/teachers.txt"));

And I create the Teacher objects like this

String inputLine;
        inputLine = inFile.readLine();
        while (inputLine != null)
        {

            teacherlist.add(new Teacher
                    (inputLine, inputLine, inputLine, inputLine,));
            inputLine = inFile.readLine();
        }

But this only works if all the fields are strings not a mix of strings and variables and objects. The fields in the text file are 1 line to a teacher and separated by :

Can someone point me in the right direction here?

Do I need to cut up the strings as I read them in ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:08:09+00:00Added an answer on May 24, 2026 at 11:08 pm

    Yes. You need to use String#split() to split the strings once you read them, create your objects and then add them to your arraylist

    List<Teacher> teachers = new ArrayList<Teacher>();
    
    BufferedReader inFile = new BufferedReader (new FileReader 
               ("pathtofile/teachers.txt"));
    
    String inputLine;
    while ( (inputLine = inFile.readLine() ) != null) { // read one line at a time
    
        String[] teacherVars = inputLine.split(":"); // split into a string array.
        teachers.add(new Teacher(teacherVars[0], teacherVars[1], teacherVars[2], teacherVars[3]);
    }
    

    Not very clear from your question how the Courses object is represented in your text file. If thats variable length on the same line, you might have to do some additional magic to dynamically create the Courses object and then create the Teacher object.

    Your instance variables inside class Teacher and Courses should not be static either. Static means class level and not object level.

    If you have a Teacher class that looks like this ,

    public class Teacher {
    
        private String teacherNo;
        ...
        private List<Course> courses;
    
        public List<Course> getCourses() {
           if ( courses == null)
              courses = new ArrayList<Course>();
           return courses;
        }
    }
    

    then, you could call it like this within your while loop —

    while ( (inputLine = input.readLine() ) != null ) {
          Teacher teacher = new Teacher(teacherVar[0], teacherVar[1], teacherVar[2]);
          Course aCourse = new Course(teacherVar[3], teacherVar[4]);
    
          teacher.getCourses().add(aCourse);
    }
    

    This is assuming your teacher line in text has a course code and course name as part of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.