Unable to display Tag
<p>Okay I am following on another question i posted a week ago. 
<a href="http://stackoverflow.com/questions/13855988/unable-to-parse-character">Unable to parse ^ character</a></p>

<p>My current situation is that now I am able to parse the required Regex and create subsequent elements. I have created tags for each element type .</p>

<p>For eg my input string is A|1|2|3^4|B||1|2|3^4^5</p>

<p>So my tags in this string are A and B</p>

<p>My Output shall be (expected)</p>

<pre><code>A1      1 
A2      2
A3.1    3 
A3.2    4
B1      (BLANK) //not B
B2      1
B3      2
B4.1    3
B4.2    4
B4.3    5
</code></pre>

<p>The reason B1 should be blank because input is B||1|2.
My current output is coming to be </p>

<pre><code>element1    A
element2    1
element3.1  2
element3.2  3
element4    4
element5    B
element6     
element7.1  2
element7.2  3
element7.3  4
element7.4  5
</code></pre>

<p>Basically, I am trying to construct an HL7 parser. replacements with the accurate tags are done to avoid confusion and maintain confidentiality. The code is as below.</p>

<blockquote>
  <p>public class Parser {</p>

<pre><code>public static final String ELEMENT_DELIM_REGEX = "\\|";

public static void main(String[] args) {
    String input = "A|1|2|3^4|";
    String[] tokens = input.split(ELEMENT_DELIM_REGEX);
    Element[] elements = new Element[tokens.length];
    for (int i = 0; i &lt; tokens.length; i++) {
        elements[i] = new Element(i + 1, tokens[i]);
    }
    for (Element element : elements) {
        System.out.println(element);
    }
}
</code></pre>
  
  <p>}</p>
</blockquote>

<p>and</p>

<pre><code>Element.java
</code></pre>

<blockquote>
  <p>public class Element {</p>

<pre><code>public static final String SUB_ELEMENT_DELIM_REGEX = "\\^";

private int number;

private String[] content;

public Element(int number, String content) {
    this.number = number;
    this.content = content.split(SUB_ELEMENT_DELIM_REGEX);
}

@Override
public String toString() {
    if (content.length == 1) {
        return "Element " + number + "\t" + content[0];
    }
    StringBuilder str = new StringBuilder();
    for (int i = 0; i &lt; content.length; i++) {
        str.append("Element " + number + "." + (i+1) + "\t" + content[i] + "\n");   
    }
    // Delete the last \n
    str.replace(str.length() - 1, str.length(), "");
    return str.toString();
} }
</code></pre>
</blockquote>
