StringBuffer replace question

Hi,

I'm trying to take a stringBuffer, make a match on part of it, and replace all instances of the match with another string.

I was able to do it replacing one name with another, but when I try the same technique for replacing "o" with "oo" I get an infinite loop, and I can't figure out why. Have searched tutorials and forum and couldn't find the answer.

here's the code:

publicclass BufTest{

publicstaticvoid main(String[] args){

StringBuffer test =new StringBuffer("Hi Larry, what's up Larry, come on over, Larry.");

String match ="Larry";

String replace ="Bill";

System.out.println("unaltered value of 'test': " + test);

System.out.println("value of 'replace': " + replace);

// see if I can isolate out 'larry'

String temp = test.substring(test.indexOf(match), (test.indexOf(match) + match.length()));

System.out.println("value of 'temp': " + temp);

// if the match is made, test.indexOf(match) will have a value

System.out.println("value of test.indexOf(match): " + test.indexOf(match));

//if the match is not made, the value will be -1

System.out.println("value of test.indexOf(replace): " + test.indexOf(replace));

//replace larry with bill

while (test.indexOf(match) != -1){

test.replace(test.indexOf(match), (test.indexOf(match)+ match.length()), replace);

}

System.out.println("value of 'test' after replace: " + test);

// the o and oo thing

StringBuffer test2 =new StringBuffer("Would you help me with my homework John, would you John? Why not John?");

String match2 ="o";

String replace2 ="oo";

System.out.println("unaltered value of test2: " + test2);

//find an 'o'

String temp2 = test2.substring(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()));

System.out.println("value of temp2: " + temp2);

System.out.println("value of index for o: " + test2.indexOf(match2));

test2.replace(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()), replace2);

System.out.println("value of test2 after replace: " + test2);

System.out.println("value of index: " + test2.indexOf(match2));

test2.replace(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()), replace2);

System.out.println("value of test2 after 2nd replace: " + test2);

System.out.println("value of index: " + test2.indexOf(match2));

// inside a while loop, the statement below causes an infinite loop

//System.out.println("inside while loop: " + test2.indexOf(match2));

}

}

it produces this on the console:

unaltered value of 'test': Hi Larry, what's up Larry, come on over, Larry.

value of 'replace': Bill

value of 'temp': Larry

value of test.indexOf(match): 3

value of test.indexOf(replace): -1

value of 'test' after replace: Hi Bill, what's up Bill, come on over, Bill.

unaltered value of test2: Would you help me with my homework John, would you John? Why not John?

value of temp2: o

value of index for o: 1

value of test2 after replace: Woould you help me with my homework John, would you John? Why not John?

value of index: 1

value of test2 after 2nd replace: Wooould you help me with my homework John, would you John? Why not John?

value of index: 1

thanks,

bp

[4602 byte] By [badpersona] at [2007-11-26 21:17:50]
# 1
If you're going to replace a character you're searching for with another sequence which also contains that same character, and you keep looking for that character from the beginning of the string instead of where you left off (hint, hint), then of course you'll have a problem.
warnerjaa at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 2
You don't show the loop that gives problems so one has to guess. My guess is that you replace 'o' with 'oo' so the string becomes longer and your find starts just before the 'o' you have added. Of course it will then replace that 'o' with 'oo' and so on.
sabre150a at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 3

okay,

came up with this:

int flag = test2.indexOf(match2);

while (flag<test2.length()){

System.out.println("value of flag: " + flag);

test2.replace(flag, (flag + replace2.length()), replace2);

// flag needs to find a single 'o' at this point:

flag = flag + replace2.length();

}

System.out.println("value of test2 after replace: " + test2);

After the replace, I need flag to find the beginning of the first single 'o' it encounters instead of incrementing by the length of replace2;

I got this on the console:

value of index: 1

value of flag: 1

value of flag: 3

value of flag: 5

value of flag: 7

value of flag: 9

value of flag: 11

value of flag: 13

value of flag: 15

value of flag: 17

value of flag: 19

value of flag: 21

value of flag: 23

value of flag: 25

value of flag: 27

value of flag: 29

value of flag: 31

value of flag: 33

value of flag: 35

value of flag: 37

value of flag: 39

value of flag: 41

value of flag: 43

value of flag: 45

value of flag: 47

value of flag: 49

value of flag: 51

value of flag: 53

value of flag: 55

value of flag: 57

value of flag: 59

value of flag: 61

value of flag: 63

value of flag: 65

value of flag: 67

value of flag: 69

value of flag: 71

value of test2 after replace: Woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo>

badpersona at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 4

try:

int flag = 0

while (flag<test2.length()){

if (flag==0)

flag = test2.indexOf(match2);

else

flag = test2.indexOf(match2,flag+replace2.length()); //or +1 more

System.out.println("value of flag: " + flag);

test2.replace(flag, (flag + replace2.length()), replace2);

}

System.out.println("value of test2 after replace: " + test2);

>

hellbindera at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 5
Hi,thanks for the reply. I tried running that and got an out of bounds exception.bp
badpersona at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 6

You got SIOBE because indexOf reached end of string returning -1

Try:

int flag = 0

while (flag<test2.length()){

if (flag==0)

flag = test2.indexOf(match2);

else

flag = test2.indexOf(match2,flag+replace2.length()); //or +1 more

System.out.println("value of flag: " + flag);

if (flag>0 && flag<test2.length())

test2.replace(flag, (flag + replace2.length()), replace2);

else

break;

}

System.out.println("value of test2 after replace: " + test2);

>

hellbindera at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 7
One more thing:thistest2.replace(flag, (flag + replace2.length()), replace2);should be:test2.replace(flag, (flag + match2.length()), replace2);
hellbindera at 2007-7-10 2:56:21 > top of Java-index,Java Essentials,New To Java...
# 8

okay, thanks that worked!

the question I have, is that it makes a match repeatedly during the while loop, but then doesn't do the replace until flag is -1.

I don't understand how the test2.replace command replaces every instance; I was thinking I would need to find test2.indexOf(match2), then replace, then find the nex test2.indexOf(match2) replace again, then when flag = -1, it meant it didn't find a single 'o' and the program was done.

bp

badpersona at 2007-7-10 2:56:22 > top of Java-index,Java Essentials,New To Java...
# 9

> okay, thanks that worked!

>

> the question I have, is that it makes a match

> repeatedly during the while loop, but then doesn't do

> the replace until flag is -1.

Sorry I have absolutely no idea what do You want ot ask about.

> I don't understand how the test2.replace command

> replaces every instance; I was thinking I would need

> to find test2.indexOf(match2), then replace, then

> find the nex test2.indexOf(match2) replace again,

> then when flag = -1, it meant it didn't find a single

> 'o' and the program was done.

You needed to find next index of match2, but from the point You have found previous one and use indexOf(searchedString,previousOccurence+searchedString.length)

indexOf(searchedString) gives You index of first occurence (or -1 if not found)

hellbindera at 2007-7-10 2:56:22 > top of Java-index,Java Essentials,New To Java...
# 10

StringBuffer sb = new StringBuffer("Would you help me with my homework John, would you John? Why not John?");

log.debug(Pattern.compile("o").matcher(sb).replaceAll("oo"));

Output:

1[main] DEBUG javaforumsapps.StringBufferReplace - Woould yoou help me with my hoomewoork Joohn, woould yoou Joohn? Why noot Joohn?

;-)

Message was edited by:

benubach

benubacha at 2007-7-10 2:56:22 > top of Java-index,Java Essentials,New To Java...