1

I want this function to replace the '@'s and '#'s with the words from the string array and output a list.

import java.util.ArrayList;
import java.util.List;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String strSpecialties = "hello, test, test2, test3";
        strSpecialties.trim();
        String []lstSpecialties = strSpecialties.split(",");

        String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";

        for(int i=0; i< lstSpecialties.length; i++){

            newString = newString.replace("#", lstSpecialties[i]);
            newString = newString.replace("@", lstSpecialties[i]);
            System.out.println(newString);
        }
    }
}

ouput:

<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />

what i want

<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest" AggregateColumn="Test" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest2" AggregateColumn="Test2" />

5 Answers 5

3

This will only work once, because after the first iteration you have replaced the @s and #s with the values. To get it to work, you need a local copy of the variable inside your for loop.

Your code should look like

for(int i=0; i< lstSpecialties.length; i++){
    String replaceStr = newString;

    replaceStr = replaceStr .replace("#", lstSpecialties[i]);
    replaceStr = replaceStr .replace("@", lstSpecialties[i]);
    System.out.println(replaceStr );
}
Sign up to request clarification or add additional context in comments.

Comments

1

try now:

for(int i=0; i< lstSpecialties.length; i++){
    String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" "                                  +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";
    newString = newString.replace("#", lstSpecialties[i]);
    newString = newString.replace("@", lstSpecialties[i]);
    System.out.println(newString);
}

Comments

1

You need to reset newString to what it originally was.

The following should work for you:

String originalString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";

String newString = originalString;

for(int i=0; i< lstSpecialties.length; i++){
    newString = newString.replace("#", lstSpecialties[i]);
    newString = newString.replace("@", lstSpecialties[i]);
    System.out.println(newString);
    newString = originalString;
}

Comments

1

Move the initialization inside the loop:

import java.util.ArrayList;
import java.util.List;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String strSpecialties = "hello, test, test2, test3";
        strSpecialties.trim();
        String []lstSpecialties = strSpecialties.split(",");

        for(int i=0; i< lstSpecialties.length; i++){

            String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";

            newString = newString.replace("#", lstSpecialties[i]);
            newString = newString.replace("@", lstSpecialties[i]);
            System.out.println(newString);
        }
    }
}

Comments

1

You need to start again with a fresh copy of 'newString' in each iteration.

Currently you do

for(int i=0; i< lstSpecialties.length; i++){

        newString = newString.replace("#", lstSpecialties[i]);

Here newString no longer contains a '#' char

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.