wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Confused with file read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused with file read" Watch "Confused with file read" New topic
Author

Confused with file read

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey yall, my assignment this week is to Implement a class named Course with Data members: String name and students (array of 30 Student representing students in the course)
and int size, which represents the current number of students in the course
I am supposed to read data from a filename, create respective students then add to the array student. Each line of this file will start with a String (no spaces between first name and last name) then followed by 4 decimal numbers for the scores. Then I am supposed to write all of the students in the array, with their scores to a file. This is my first time with writing to a file, and we are supposed to use exceptions.
Anybody have any ideas on this one? I just can't figure out how to use the getname and setname methods in Student, and Course to establish this. Thanks in advance.
Here is my code so far, It is not complete....

[ April 26, 2003: Message edited by: Steve Wysocki ]
[ April 27, 2003: Message edited by: Steve Wysocki ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok, I got it to read from the file, but I cant seem to input the grades.
I added the code:

but now, I am getting the error that "Course.java": Error #: 300 : constructor Student(java.lang.String, double) not found in class exercise7.Student
When it is. It says double and I know that I am supposed to pass an array into the Student constructor.
Any ideas would be appreciated
again my goal here is just to read the file of 30 students and add their grades into the array as well and print out the info and then dump that info to another file. I haven't started the last part yet, but nobody has posted on this thread so Im still chugging along.
Thanks
Steve
[ April 27, 2003: Message edited by: Steve Wysocki ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi steve,
as u know the format of the student input file u 've which is,
STUDENTNAME GRADE1 GRADE2 GRADE3 GRADE4
u can do follwing in the while (line != null )...
//1. frist token is student name,
String tempName = stk.nextToken();
//2. create empty array of double that can hold 4 grades
double[] grades = new double[4];
//3. read each grade and parse for double format
grades[0] = Double.parseDouble(stk.nextToken());
grades[1] = Double.parseDouble(stk.nextToken());
grades[2] = Double.parseDouble(stk.nextToken());
grades[3] = Double.parseDouble(stk.nextToken());
// 4. create a student object and put in the array
students[count] = new Student(tempName, grades);
//5. increment student counter
count++;
// it is a good practice to put count++ outside the students[count] instead of writing students[count++]...makes debugging easier for us..

hth,
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi steve
as this is a simple assignment we dont have to consider OO things otherwise we would have some different design than the existing one..
we would have following objects,
1. Student - that represents a student
2. Course - that represents a course information
3. CourseCatalog - that represents a listing of courses for a particular semester (say fall, spring etc..) and will have listing of students enrolled in the course for that semester.
the reason of having the CourseCatalog is - in real world we have,
1. different semesters might be offering different courses,
2.across semesters we might have changed professors for the same coures (u know ..Prof A taking Course-1 in fall but in spring Prof B is taking the course...and all)
well, this is my 2cents about what i had first in mind...we dont have to worry about these things in simple assignments but certainly better to keep this in mind..
regards
maulin
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Thanks Maulin, that sure did help. But now my output is all messed up, here is an example of my first two students. Im just wondering why it prints out the grades and then the student name.
I'll have to check that out. I'm sure it has something to do with my toString method. Thanks again Maulin, you are the only one to help me on this project and I sincerely thank you for that.
76.055.099.677.4
Students name is: JohnSmith
Students grades are:
77.985.466.798.5
Students name is: HeideAdams
Students grades are:
Never mind, duh, the toString for loop didn't have my info string in it.
my bad.
[ April 27, 2003: Message edited by: Steve Wysocki ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confused with file read