944,025 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9370
  • Java RSS
Nov 18th, 2004
0

loop to create arrays when reading a file

Expand Post »
I'm having a bit of trouble working out what sort of loop to use in a program I'm writing.

Basically, I have a file with 2 lists of numbers separated by ";" e.g.

Name ;Magnitude

2713.0;21.546
2713.0;19.564
2713.0;20.102
2713.0;20.959
2714.0;22.031
2714.0;18.46825
2714.0;15.323
2715.0;22.808
2715.0;19.345
2715.0;21.357
2716.0;21.473

on so on up to 4076!

What I want to do is to collect together all the magnitudes that have the same name (i.e. 2714) and put them in an array so I can sort the order of them. (so I have one array containing all the magnitudes for 2713, one for 2714, etc)

I'm reading in one line at a time in a while loop and separating using StringTokenizer. But I can't work out how to put in a loop (or something) that will put the magnitude into an array if its name equals the first number, and if not, increase the number to be the same as the next name and start a new array.

Would be great if someone could help me out! Don't know if it can be done or if I'm making it too complicated. Hope I'm making some sense!

Cheers
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ld834 is offline Offline
2 posts
since Nov 2004
Nov 18th, 2004
0

Re: loop to create arrays when reading a file

1. Open file
2. Read line
3. Save values (Name ;Magnitude)
4. Save Name to save variable (saveName)
5. Create Array
6. Store values in array
7. Begin loop
8. Read file
7. If end of file clean up and close
9. If Name == saveName Store values in array
10. else Name <> saveName
-- create new array and store values in new array
-- Save Name to saveName variable
11. return to step # 7

I suspect you would want to do a collection to store the values (as opposed to an array. The reason for this is an array is a fixed size when defined, so there would be no way to re-diminsion the array if you have an inknown number of Names.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 18th, 2004
0

Re: loop to create arrays when reading a file

Better way:
Use a Map with a String representation of the name.
As the values store Lists of magnitudes.
Then you can just do ((List)map.get(name)).add(magnitude) for each record.

You can't increase the size of arrays so using other data structures is a requirement.
As Map and List have what you want why not use them
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 18th, 2004
0

Re: loop to create arrays when reading a file

Quote originally posted by jerbo ...
1. Open file
2. Read line
3. Save values (Name ;Magnitude)
4. Save Name to save variable (saveName)
5. Create Array
6. Store values in array
7. Begin loop
8. Read file
7. If end of file clean up and close
9. If Name == saveName Store values in array
10. else Name <> saveName
-- create new array and store values in new array
-- Save Name to saveName variable
11. return to step # 7

I suspect you would want to do a collection to store the values (as opposed to an array. The reason for this is an array is a fixed size when defined, so there would be no way to re-diminsion the array if you have an inknown number of Names.
Opps, step 11 sould return to step # 8
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 18th, 2004
0

Re: loop to create arrays when reading a file

Thanks for your help!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ld834 is offline Offline
2 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Your Homework
Next Thread in Java Forum Timeline: Why Doesn't My Sort Work!?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC