Hello All,

I would like to create a Student class as follows:

class Student
{
String name;
int age;
int studentNum;
String instructor;
}


My question is how would I read information from a text file into an array of class Students?

The file is as follows:

Sue Jones, 19, 897680, Mrs. Smith
John Smith, 21, 909897, Mr. Jones...

Afterwards I would like to be able to input the studentNum and get the other information about the student as well.

Any help would be great.

Thank you,

Recommended Answers

All 3 Replies

Just parse the lines from the text file into its fields, create the Student, and I would suggest you could use a HashMap if you want to access the Students by their ID.

You have already posted several threads about reading a field from text file, so I find it hard to believe you cannot see a starting point for this. Post your code if you have problems with the implementation.

Thanks Ezzaral for your help. I am basically learning as I go along. You are right, at this point I know how to read a text file, however, I am a bit confused about reading the information into "class Student" for output later through a search.

I will work on your suggestion and again thank you for your help.

Well, if you have parsed the fields into an array for each line, such as with split(","), then you can just create the new Student object and set each property from your field array. Then add the Student object to a HashMap with the id field as it's key, ie studentMap.put(aStudent.studentNum, aStudent); Student objects can them be retrieved from the map by id.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.