Java... Scanner, Array of Objects, Input from file

Status
Not open for further replies.

Toshiro

In Runtime
Messages
115
Anyone here good at Java? I'm working on an assignment for my computer science course and I've run into a bump. I need to read in the following exact text from a file:

Code:
LastName,FirstName, Exam1, Asg1, Asg2, Exam2, Asg3, Asg4
Karr, Arlen, 91, 86, 94, 100, 98, 93
Stotz, Ralph, 81,83,,93, 78
Yi, Yu, 99, 88, 101, 76, 90, 94
Rao, Sista, 91, 86, 94, 100, 98, 93
Christopher, Thomas, 78, 79, 82, 88, 78, 91
McClurg, Andrew, 91, 87, 99, 87,,93
Noble, Rich, 84, 79, 85, 88, 90, 91
Cole, John, 100, 100, 100, 100, 100, 100

I need to parse this so that the first line is ignored. From there, the first two chunks of each line will be parsed in as first name then last name strings, and the following numbers will be parsed in as integers for variables holding grades. I must be able to keep the names and grades synchronized so that I can sort the list by last name and everything will still match up. Also, missing numbers are to be interpreted as zeroes.

So far, everything I've tried has failed, and I've searched all over the Intarnets and haven't found anything I can actually use.

I want to say I should be able to parse the text by skipping the first line entirely, then scan each chunk into separate array object variables. I've got a Student class that holds each variable as shown in the text above.

One problem is that I need to use whitespace (including newline) AND commas as delimiters so it drops everything but the names and the numbers.

The other problem is that I can't figure out how to assign input strings and integers to instance variables like students[index].exam1 = scanFile.nextInt(); or something (assuming exam1 is an instance var of students[index] and scanFile is a Scanner object) when the instance variable is inside an object which is part of an array.

Any ideas? I'm getting rather frustrated with Java.


This is the code I'm using to declare/initialize my array of 10 StudentRecord objects:
Code:
StudentRecord[] students = new StudentRecord[10];
for (int index = 0; index < 10; index ++)
{
     students[index] = new StudentRecord();
}

This is the code I had been trying to use to fill variables after trying to skip the first line of input text from the file:
Code:
while(scanFile.hasNext())
{
    students[index].lastName = scanFile.next();
    students[index].firstName = scanFile.next();
    students[index].exam1 = scanFile.nextInt();
    //...and so forth until .asg4
    index++;
}
 
Status
Not open for further replies.
Back
Top Bottom