Question..

I'm importing a text file with information regarding student information. The code below works perfectly when importing the contents of that text file. The text file contains several lines of information including the student's name, the number of courses he or she is registered for, and the course name and its number level.

For instance, a piece of the text file may read:

John H Doe<== Student Name

3 <== Number of courses currently registered for

MATH 250 3<== Math course with level 250 and worth 3 credit hours

MATH 117 3<== Another math course with level 117 and worth 3 credit hours

ENGL 201 3<== English course with level 201 and worth 3 credit hours

The code that I've come up with below takes the course names, course level, and credit hours and reads them as a String, int, and another int value, respectively, and stores them as variables. I need to sort these first by course name and then by course level. I'm smelling some nested if's with some nested sorts but I am at a standstill.

What would be an easy way to do this? I'm sure I can sort the course names without a problem, but how can I sort the course numbers? Would I use some sort of nested sort? And if so, how would I know I'm sorting the course levels for the two MATH courses and not including the course level for the ENGL class?

Also, note that in

and out

are part of an in-house utility, so not to confuse anyone. :-)

This portion of code is executing and loops through the text file reading the various data. I've omitted the rest of the code.

while (!in.eof()){

in.readWord();

String firstName = in.lastString();

System.out.println("First Name: " +firstName);

in.readWord();

String middleInitial = in.lastString();

System.out.println("Middle Initial: " +middleInitial);

in.readWord();

String lastName = in.lastString();

System.out.println("Last Name: " +lastName);

in.readLine();

in.readInt();

int numCourses = in.lastInt();

System.out.println("Number of Registered Courses: " +numCourses);

in.readLine();

for (int i = 1; i <= numCourses; i++){

in.readWord();

String courseName = in.lastString();

System.out.println("Course Name: " +courseName);

in.readInt();

int courseNum = in.lastInt();

System.out.println("Course Num: " +courseNum);

in.readInt();

int creditHours = in.lastInt();

System.out.println("Credit Hours: " +creditHours);

in.readLine();

}

out.blankLine(1);

}

[3115 byte] By [SittingDuck] at [2007-9-27 21:48:12]
# 1

Good evening.

The easiest way to do this is getting the sort order array.

An example:

NameLevel# Hours

OSes810

English324

Java123

Sorting by name, it would give {1, 2, 0}.

(second first, third second, first third)

Sorting by level, it would give {1, 0, 2}.

(second first, first second, third third)

Once you've got the order array, you need to apply the order to all your arrays.

To solve your second (smaller) problem, sorting by name, then by level, requires you to sort first by level, then by name. Make sure you implement a natural sort (equal values keep in the same order).

Success,

Levi

levi_h at 2007-7-7 3:47:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Hello Levi,

Thanks for the tip. Although I'm not quite sure if I'm catching your meaning.

I'm at a point right now where I'm stuck on how to take those students from the text who are registered for more than one class and storing the course information of the second, third, etc. course. For the students who are only registered for one class, this doesn't present a problem. But for those who are registered for more than one, I'm not sure how to add those into the class Student which I have created to model a student. In that Student class are places to set and store the variable information that is read from the text file. I'm not sure how I can go about assigning more than one course to a student. I'm guessing that an array would be used here?

I will have a list of students, here I'll call that StudentList which models a group of Students. A generic list template has been written in-house for use which will just be renamed to StudentList. In the Student class, I only have one place to set the name of the course and the course number (courseName) and (courseNumber). I'm not sure how I can go about coding the class so it will accept more than one course for the Student.

Am I making any sense? I'm confusing myself more as I go along! haha

SittingDuck at 2007-7-7 3:47:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
Would anyone else happen to have any ideas on this?Any help would be greatly appreciated. :)
SittingDuck at 2007-7-7 3:47:25 > top of Java-index,Archived Forums,New To Java Technology Archive...