• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting data from 2 files into one or more arrays  RSS feed

 
JT Harris
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I will be up front about this I'm asking for help with my homework cuz I got to be honest I'm struggling with this right now.
I'm supposed to write a program that takes data from 2 files the first file has student id and name and is in student id order. the 2nd file has student id, class hours, and grade(real number) is in student number sequence.

What I need help with is 1. how to read from 2 files and do I need to do it in any special order? 2. how do I get the names from the first file into an array with the rest of the data lined up with the correct student id's? So this is where I'm at, normally I would try to figure it out on my on but my other finals took up all my time and i'm in a bit of a time crunch right now. Any help would be greatly appreated and I'm sure I'll have more questions as this goes on.
Jack Jack
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You haven't told us how much Java you know, so that makes it more difficult to make suggestions.
I think you need a Student class, with name number hours and grade as fields. You would start with only name and number, since hours and grades are not know until later.
So you can read the first file and create Student objects, then set their grades from the second file.
If you are using an array, you need to know how many lines there are in the file before trying, so you can set the size of the array.
You can read from the 1st file, and put the objects into the array.
Then you would have to go through the second file, find the objects in the array which correspond, and set the grades and hours.

What does it say about sorting? Are you supposed to implement your own sorting algorithms?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you can have open as many files as you need at any given moment. For each file, you create a separate 'handle', and then you can read any of them at any time.

If you want to keep it simple, create a class that lets you add all the data pieces. then, I'd go through one of the files. Build the info you can, and store each in a hash table, probably using the student ID as your key.

When you read the second file, you will read the ID. use that to pull the correct object out of the hash, and update the fields accordingly.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be nice to use a Map, as you suggest, Fred, but I suspect JTH may be required to put his objects into an array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!