Question

I have written a simple program to parse a string of format field1(sf1),field2(sf1,sf2),field3[Detail],field4(sf1,sf2).

private void tokenizeUsingDeque(final char[] array, final List<String> container) {
  final Deque<Character> stack = new ArrayDeque<>();
  boolean leftBraceFound = false;
  final StringBuilder builder = new StringBuilder();
  for(int i = 0; i &lt; array.length; i++) {
    stack.addLast(Character.valueOf(array[i]));
    if(array[i] == '(') {
      leftBraceFound = true;
    }
    if(array[i] == ',') {
      if(!leftBraceFound) {
        stack.removeLast(); // Do not add the comma.
        builder.setLength(0);

        // Pop until stack is clear
        for (final Character nextChar : stack) {
          builder.append(nextChar);
        }

        container.add(builder.toString());
        stack.clear();
      } 
    }

    if(array[i] == ')') {
      if(leftBraceFound) {
        builder.setLength(0);

        // Pop until stack is clear
        for (final Character nextChar : stack) {
          builder.append(nextChar);
        }

        container.add(builder.toString());
        stack.clear();
        leftBraceFound = false;
      } 
    }
  }

  if(!stack.isEmpty()) {        
    // Pop until stack is clear
    for (final Character nextChar : stack) {
      builder.append(nextChar);
    }
    container.add(builder.toString());
    stack.clear();
  }
}

Strangely, for the string mentioned above, the container array list contains following elements (notice the extra empty characters)

container(0) --> "field1(sf1)"
container(1) --> ""
container(2) --> "field2(sf1,sf2)"
container(3) --> ""
container(4) --> "field3[Detail]"
container(5) --> "field4(sf1,sf2)"

Can someone help me understand why those extra space characters are coming?

Thanks, NN

Was it helpful?

Solution

The problem is that you're adding a string to the container twice for each string in the input.

When you encounter a '(', you enter the string to the container, and reset the 'leftBraceFound' flag.

Immediately after that, you encounter a ',' while the flag is reset, so you enter the 'if' statement block. By this time there's nothing on the stack except the comma character, which you pop out, and then you create an empty string and add it to the container.

You should change the "if(array[i] == ',')" block to not add a new string, or if you anticipate strings between commas not wrapped in parentheses, you'll need to alter the loop to check between commas and ignore parentheses altogether.

OTHER TIPS

You added text into container after ")" and after ",". After ")" you added correct text and reset "stack". Then you get "," and add "stack" to container, but stack is empty at this moment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top