Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Anybody wanna help w. a Java assignment?

Anybody wanna help w. a Java assignment?
Thread Tools
Senior User
Join Date: Feb 2003
Location: Pittsburgh, PA
Status: Offline
Reply With Quote
Feb 7, 2007, 03:32 PM
 
Ok, well the assignment is to read from a file into an array of strings(whose length is, I think the same as the first line of the file) then I'm assuming tokenizing this to pull out values from each line then take the numerical values average them, sort the names and spit it out.

12
Doe, Joe:78:69:83:72

That's an example from the file, 12 is the number of names listed, then a name, and 4 numbers that need to be extracted out of the array and averaged.

I've managed to get myself totally lost, weee.

a) How the hell do you read a file via Scanner? I know this shouldn't be this hard but I just can't figure it out.

b) How do I read the first line of the file and have that be the length of the array of strings? I don't think this that hard and I think I've sort of figured this out, but geeez.

c) Then how do I continue to read the file starting at the second line filling in the array? This is really where I get lost.

I imagine that if I can figure this much out I can feel my way through the rest of it.

Thanks!
15" MacBook Pro 2.0GHz i7 4GB RAM 6490M 120GB OWC 6G SSD 500GB HD
15" MacBook Pro 2.4GHz C2D 2GB RAM 8600M GT 200GB HD
17" C2D iMac 2.0GHz 2GB RAM x1600 500GB HD
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 8, 2007, 07:25 AM
 
[codex]
Scanner scanner = new Scanner(new File(...));
int count = Integer.parseInt(scanner.nextLine());
String[] arrayOfStrings = new String[count];
for (int i = 0; i < arrayOfStrings.length; i++) {
arrayOfStrings[i] = scanner.nextLine();
}
scanner.close();
// Do something with the array of strings
[/codex]

Answers a, b and c. The rest is up to you.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 02:46 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2