Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Java... Scanner, Array of Objects, Input from file
Closed Thread
Old 02-25-2007, 11:12 PM   #1 (permalink)
 
True Techie

Join Date: Apr 2004

Posts: 115

Toshiro

Send a message via AIM to Toshiro Send a message via Yahoo to Toshiro
Default Java... Scanner, Array of Objects, Input from file

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++;
}

__________________
Make: eMachines
OS: WinXP Pro
Mobo: Gigabyte 8VM533M-RZ
CPU: 2.2GHz Intel Celeron
Vid: Gigabyte NVidia MX 4000 (128 MB DDR, TV out)
Sound: onboard
RAM: 512MB DDR
HD: 40GB (1:3 partitioned), 34GB
Drives: DVD R/RW and 16x DVD-ROM
Browser: FireFox
ISP: AT&T/SBC Yahoo! DSL
Toshiro is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On