• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading a file and displaying its content on screen  RSS feed

 
Arnon Neder
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

For instance, the input can be (explanations are in brackets but not in the actual file):
505 (student number)
efron (last name)
ben (first name)
3 (no. of exams)
88 (1st exam mark)
70 (2nd exam mark)
100 (3rd exam mark)
38(student number)
agron (last name)
sally (first name)
5 (no. of exams)
78 (1st exam)
60 (2nd exam)
90 (3rd exam)
88 (4th exam)
89 (5th exam)
101( student number)
... and so on

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?

Thanks
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to create a Student class which has class members for number, first name, last name, number of exams, and an array of exam marks. Then you could add the student instances to a collection, and use a custom Comparator to sort them as needed.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings.

I asked a similar question not so long ago. Campbell gave me a link to the Collections trail. It's a lot of reading, but it should get you pointed in a good direction.

Regards,
Robert
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!