I’d like to perform a function on a a list of data in at text file but I’m not sure how to go about it. My understanding of how to manipulate arrays and strings is weak, that is why I’m doing this project.
My text file looks like:
0, 0. 50 0.5 66;
1, 0. 69 2. 70;
2, 0.5 48 0.5 71;
3, 1. 47 1. 75;
4, 2. 52 1. 74;
5, 2. 71 1. 80;
6, 3. 65 1. 61;
7, 4. 62 1. 68;
I’m looking for a result like this:
0, 0. 50 0.5 66 0. 69 2. 70;
1, 0.5 48 0.5 71;
2, 1. 47 1. 75;
3, 2. 52 1. 74 2. 71 1. 80;
4, 3. 65 1. 61;
5, 4. 62 1. 68;
The second elements of the first two entries is the same (0.)and I’d like to combine these entries onto the same line. The same for lines 4 and 5.
As I understand I must read my file into an array list, but I don’t know how to query the array list to find all entries that have the same value for the second element, and then merge the entire entry with another entry.
I have no code to show because I don’t know where to begin.
I appreciate any input.
I wouldn’t use a list, i would use a map. A Map has a method
So, if you are interested in the second column of a given line, I would do something like (pseudocode)
You might have to tweak this (for example, store the result of the merge to put in the lookup), what you are doing here is leveraging the fact that for a Map, put and get operations are really fast (a HashMap uses the hashCode method that is on every object). So you can easily determine if you already have a line with the field you are looking for, since you use the field of interest as the key, to look up the like that the field is in.