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]

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?
> 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?