## ld834

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

## jerbo

1. Open file
3. Save values (Name ;Magnitude)
4. Save Name to save variable (saveName)
5. Create Array
6. Store values in array
7. Begin loop
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

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.

## jwenting 1,649

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 :)

## jerbo

1. Open file
3. Save values (Name ;Magnitude)
4. Save Name to save variable (saveName)
5. Create Array
6. Store values in array
7. Begin loop
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