Question: Okay, Now I am able to parse space which was my previous problem. Now my parser is almost ready but has a defect which I am unable to figure out.

I am able to retrieve Data after segments(see code) and data in between pipes.
What I am not able to get to is a level up and retrieve data which is contained between pipes and seperated by ^.

Eg.

```
Input String is A|1|2|3^4|
Expected Output
element1        A
element2        1
element3        2
element4.1      3
element4.2      4

```

However, My current output is coming to be 

```
element1        A
element2        1
element3        2
element4       3^4

```

I am recieving exception
1 [Ljava.lang.String;@1786e64 with various ids after @

The commented code given below is creating problem.

CODE:

```
Scanner scanner = new Scanner(System.in);
str=scanner.nextLine();
System.out.println(str);
segments="(A)|(B)|(C)|(D)";
pipe="[\\s+\\|+\\+]";
carat="[\\^+]";
for(i=0;i<token_type1.length;i++)
{
token_type2=token_type1[i].toString().split(pipe);
for(j=0;j<token_type2.length;j++)
{           
/*
token_type3=token_type2.toString().split(carat);    
for(k=0;k<token_type3.length;k++)
System.out.println("\t"+(k+1)+" "+token_type3[k]);
*/
System.out.println((j+1)+"\t"+token_type2[j]);
}
System.out.println();
}

```

Please advise.

Answer: 
This will transform your input to your desired output (save the classes in two different files)

> 
> Parser.java
> 
> 
> 

```
public class Parser {

    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 < tokens.length; i++) {
            elements[i] = new Element(i + 1, tokens[i]);
        }
        for (Element element : elements) {
            System.out.println(element);
        }
    }

}

```

and

> 
> Element.java
> 
> 
> 

```
public class Element {

    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 < 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();
    }
}

```
Scanner() by default has space as delimeter. So if you do scanner.next() again then you will get the remaining input part, '4'.

You can set your own delimeter by using `usedelimeter(String pattern)` api of Scanner class.