trouble with string modifications

i need to modify the string parameter "line" to insert a space before and after all punctuation such as ". , ' " " - ) ( ; ".

so the line System.out.print should output as System . out . print

and " I am your father. - Darth vader" into "I am your father . - Darth vader

this is what I have so far but it does not work(btw punc. is defined as string " . , ' \" ( ) ; -"

for(int i=0;(line.length()-1)>i;i++)// go through all letters to find punc.

{

char n = line.charAt(i);

System.out.print(n);

if((punc.indexOf(n))>-1)//if char is a punc. add space before and after it

{

String begin=line.substring(0,i);

String middle=line.substring(i,i+1);

String end=line.substring(i+1);

line = begin+" "+middle+" "+end;

}//end if

}//end for

[1249 byte] By [thrivea] at [2007-11-26 15:25:52]
# 1
Please define what "does not work" means.
floundera at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 2

Well, you keep modifying line as you go, thus messing up your index into the line.

I'd suggest creating a StringBuilder object (or a StringBuffer if you're using an older JDK), and append to that as you go along. When you're done, return the result of yourStringBuilder.toString() to the caller.

Also -- you are aware, aren't you, that Java passes by value, and so if you modify line within your method (that is, create a new String and assign it to line, which is what you're doing), you have to return the new line to the caller. Right?

paulcwa at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 3
when i print out " i " it just gives a infinite loop of index values.What do you mean by pass line to the caller?
thrivea at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 4

> when i print out " i " it just gives a infinite loop

> of index values.

Think about it.

Suppose it finds a punctuation mark at position 15.

It then replaces line with a new version of line, with a space at position 15, the same punctuation mark at position 16, and another space at position 17.

Then i is incremented.

What will it find on position 16?

> What do you mean by pass line to the caller?

Does your method end with this:

return line;

If not, how does the changed version of line get to the code that called the method you're writing?

paulcwa at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 5
im making some progress...i created a String temp that stores the begin+" "+middle+" "+end;but this is the output i recieve System.out . print
thrivea at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 6
apparently what is happening is thisSystem . out.printthen it gets overwritten bySystem.out . print
thrivea at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 7
I need a way to append the changes made and apply it to line without the loop getting stuck on the already accounted for peice of punctuation
thrivea at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 8
> I need a way to append the changes made and apply it> to line without the loop getting stuck on the already> accounted for peice of punctuationI told you how in reply #2.
paulcwa at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 9
I dont see how stringbuffer would help me can you please explain further?
thrivea at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 10
Have you read the API for StringBuffer? It's pretty obvious how itwould be helpful to you.
es5f2000a at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...
# 11
Iterate over your string. Everytime you have a letter add it to your StringBuilder. Everytime you have a punctuation mark, add a space, add the punctuation mark add another space.
floundera at 2007-7-8 21:41:31 > top of Java-index,Java Essentials,Java Programming...