views:

562

answers:

2

I have the following string with separate delimiters that needs to be parsed out and I am running into a bit of a wall at the moment.

example:

category 1---category 2;subgroup 1||subgroup 2---subgroup 1;value 1||value2---value 3

I wanted to re-arrange into the following grouping:

category 1;subgroup 1;value1;subgroup 2;value 2;category 2;subgroup 1;value1

The "---" delimiter separates a category and its values. The "||" delimiter separates fields belonging to the same category The ";" delimiter separates the field types (category; subgroup; value) There can be N categories of which they can have N subgroups.
each value is associated 1:1 with its subgroup

The first pass through works fine but once I hit category 2 everything nulls out. I know I'm missing something obvious but I'm a little hazy right now and Java is not my first language. Here is what I have so far (sans delimiters)

StringBuilder result = new StringBuilder();
String categoryArray[] = category.split("---");
String subGroupArray[] = subgroup.split("---");
String valueArray[] = value.split("---");

 for (int counter=0; counter<categoryArray.length;counter++){


  String categoryArray2[] = categoryArray[counter].split("\\|\\|");
  String subGroupArray2[] = subGroupArray[counter].split("\\|\\|");
  String valueArray2[] = valueArray[counter].split("\\|\\|");

         result.append(categoryArray[counter].trim());
  for (int counter2=0; counter2<subGroupArray2.length;counter2++){

   result.append(subGroupArray2[counter2].trim());
   result.append(valueArray2[counter2].trim());

  }
  result.append("||");

 }
return result;



Any help or optimized approaches would certainly be appreciated!

A: 

Start with writing some junit tests given the input as a string, and the output structure you expect. Paste the whole test class here, along with your failing implementation.

Thorbjørn Ravn Andersen
A: 

Your algorithm is basically right. The problem is that String.split() works with regexes as the input, not a string. So "||" does not mean the String "||", it means whatever the regex means. So replace this:

subGroupArray[counter].split("||");

With:

subGroupArray[counter].split("\\|\\|");

And it should work.

_jameshales
You're right I did forget that | is a metacharacter and I didn't escape it properly. Thanks much. The algorithm is still failing on the second pass through though and returning nulls.
godel
Actually there was an anomaly in the data coming down-stream that ended up throwing my errors. Everything is fixed now and the algorithm works fine. I'll award you as I likely would have banged my head on that one a bit later.
godel
I got it working using the same code, but I had to fill in the definitions for `category`, `subgroup` and `value` since you haven't given anyway. I used the values from `input.split(";")`.
_jameshales