2

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.

4
  • 1
    Could you show us the expected result of the given example? Commented May 16, 2011 at 20:28
  • What does it mean to "merge" two rows? Commented May 16, 2011 at 20:28
  • I edited to include the expected result. Commented May 16, 2011 at 20:35
  • is the second columns (the data after the comma) always sorted? Commented May 16, 2011 at 20:38

2 Answers 2

5

I wouldn't use a list, i would use a map. A Map has a method

put(key, value)

So, if you are interested in the second column of a given line, I would do something like (pseudocode)

Map<String, String> lookup = new HashMap<String, String>();

for (...) {  // loop over the lines in the file

    String key = getSecondFieldFromLine(line); // implement this
    String val = lookup.get(key);


    if (val != null ) {
        merge(val, line);  // implement this according to whatever merge means
    }

    lookup.put(key, val);
}

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I would parse the input into a list of lists (or array of arrays or whatever collection you prefer). I would implement a Comparator that compares based on the second element in a list. You can then sort your main list or array. Finally, you can iterate through your list of lists, combining adjacent lists using whatever your combinatorial logic is, producing a new list as the result.

On the other hand, if your input is always going to be sorted as you are showing, then you can skip the comparator implementation and sorting altogether and just iterate through your collection performing the combinations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.