| | |
loop to create arrays when reading a file
![]() |
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
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.
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.
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
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
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
•
•
•
•
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.
![]() |
Similar Threads
- Need help reading a file (C++)
- Reading multiple lines from a txt file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- help with parrallel arrays (C++)
Other Threads in the Java Forum
- Previous Thread: Sequential search in java
- Next Thread: Why Doesn't My Sort Work!?
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






