reading a file, sorting it and displaying its content on screen

Hi,

I have a program that writes a user's input to a file.
The input consists of several loops. In each loop the user enters a student number, the student first name and last name and then several exam results. Each student has different number of exams, depend on the user input.

I need to write a program that reads that file and writes it to the screen, but it needs to do it in ascending order according to the student number.
Leaving aside the format it will be displayed on the screen, my question is how should I sort the data by the student number?
I though about first reading each student as an array, but my problem is that each array contains different number of elements (depends on the number of exams) and also each array containד both strings (first and last names) and ints.
Does anyone have an idea?

Re: reading a file, sorting it and displaying its content on screen

There are several possibilities. Assuming you have to use arrays, I would use a two dimensional array for the test scores. You keep track by treating the two dimensional array as a one dimensional array while sorting. For example:

Re: reading a file, sorting it and displaying its content on screen

I though about first reading each student as an array, but my problem is that each array contains different number of elements (depends on the number of exams) and also each array containד both strings (first and last names) and ints.

It sounds like each student is a object holding multiple types of data. What I've always done in this situtaion is create another class. Then you can create an array of objects made from that class.

So you could think of students as numbers. Number 0 in all the arrays applies to one person. Now you said you had the problem of students taking different number of exams well then you just put in a int to tell how many they took. I have the multiDimensinal Array because it would be like testScores[forWhatStudend][whichTest]. Hopefully this is clear enought and can fix your problem

Re: reading a file, sorting it and displaying its content on screen

The number of tests vary. besides, its not necessary to have a square or rectangular two-D matrix. A two-D matrix is nothing more than a a one-D matrix which holds other one-D matrices of arbitrary length.

Re: reading a file, sorting it and displaying its content on screen

Thank you all for your comments.

What I didn't mention, is that each student has a different number of subjects, and each subject has different number of exams. I thought about two dimensional array, but that sounds like it's not enough..

Re: reading a file, sorting it and displaying its content on screen

Ugh. More requirements. Now I tend to think that a classes are the way to go. You could have a Student class with appropriate information (exercise for you). Then you could possibly have a Subject class which also holds appropriate information.

Re: reading a file, sorting it and displaying its content on screen

Thanks again for your help!

I'm trying to use 3d array, but the problem is that I can only define the number of subjects for each student and number of exam for each subject while reading the file, and not initially. When I want to print on the screen, for instance, the value stored in homeworkMark[0][1][1] I can't, because the number of elements is defined inside the loop and not outside of it.

Re: reading a file, sorting it and displaying its content on screen

Is there a max limit of exams possibly taken? Becuase if so initiate the arrays with the max possible and set all of the to -1. Then when you go to print use a if statement so it only prints whats not -1. This may not be the most memory efficient but it should work Or if that doesnt work then have a loop find were the -1 and use that as a limit by having a varialbe like int limit[]

Re: reading a file, sorting it and displaying its content on screen

It doesn't matter. If you really want to use 3d arrays or 4d arrays you can use the length of each array object to determine the size of that object. And you can reallocate the array using the Arrays.copyOf() method. So you do not need any flag to signify the last used element. Actually, I would recommend using ArrayList because it does all of that for you.