Question

I am splitting the XML elements using String.split() method. The format of the XML elements are mentioned below:

start:23 | stop:43 | name:abc def

After splitting the strings, I'm trying to trim and assign like the following:

String[] parts = oneLine.split("\\s*\\|\\s*"); // splits into 3 strings
for (int x = 0; x < parts.length; x++) {
    String tmp = parts[0];
    if (tmp.startsWith("start:")) {
        tmp = tmp.substring("start:".length());
        try {
            this.begin = Integer.parseInt(tmp);
        }
        catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
    }
    else {
        System.err.println("Wrong format");
    }

    String tmp1 = parts[1];
    if ( tmp1.startsWith("stop:")) {
                tmp1 = tmp1.substring("stop:".length());
                try {
                    this.end = Integer.parseInt(tmp1);
                }
                catch ( NumberFormatException nfe ) {
                    nfe.printStackTrace();
                }
            }
            else {
                System.err.println("Wrong format"+tmp1);
            }
    Strint tmp2 = parts[2];
    if ( tmp2.startsWith("name:")) {
                tmp2 = tmp2.substring("name:".length());
                try {
                    this.name = tmp2;
                }
                catch ( NumberFormatException nfe ) {
                    nfe.printStackTrace();
                }
            }
            else {
                System.err.println("Wrong format"+tmp2);
            }

This returns an error Wrong format. Is this the problem with assignments String tmp1 = parts[0]?

Was it helpful?

Solution

Answer to my question.

            String[] parts= oneLine.split("\\s*\\|\\s*");
    //System.out.println(parts[0]);
    String tmp=parts[0].substring("start:".length());
    this.begin=Integer.parseInt(tmp);
    String tmp1=parts[1].substring("stop:".length());
    this.end=Integer.parseInt(tmp1);
    String tmp2=parts[2].substring("name:".length());
    this.uri=tmp2;

Above code snippet solves my problem

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