0

I'm trying to use StringBuffer#insert to insert a char into different positions in a word but either I'm not using it right or I've misunderstood what it is this function actually does.

First off, I want it to add the letter 't' into different positions within "Java". I've given the part of the code I'm trying to use.

For example, the first time it's run it should print "tJava", then second time "Jtava" and so on until the loop ends after it prints "Javat". However, all I'm getting is:

tJava
ttJava
tttJava
ttttJava

If I'm using it wrong or there is an alternative way of doing this suggestions would be greatly appreciated.

String addLetter  = "t";
String Word = "Java";

StringBuffer tempWord = new StringBuffer(Word);

for(int i = 0; i<Word.length(); i++) {
    tempWord = tempWord.insert(i, addLetter);
    System.out.println(tempWord);
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
NiamhMc
  • 3
  • 1
  • 1
    You're using it correctly, but you keep inserting into the same thing. So you have "1Java" "12Java" "123Java", etc. You need to insert into a new copy of `Word`. – Teepeemm Apr 19 '15 at 20:30

5 Answers5

2

When you call insert on StringBuffer the inserted value will be kept. For example :

 StringBuffer test = new StringBuffer("test");
 test.insert(1, "t");
 test.insert(3, "t");
 System.out.println(test);

would print ttetst.

So your re-assignment is not needed tempWord = tempWord.insert(i, "t"); if really you would want to keep the updated string.


Now if you want to display the original string with only 1 added character at the correct position, you will have to re-assign word to tempWord at each iteration.

String addLetter  = "t";
String Word = "Java";

StringBuffer tempWord = new StringBuffer(Word);

for(int i = 0; i<Word.length(); i++) {
    tempWord = new StringBuffer(Word);
    System.out.println(tempWord.insert(i, addLetter));
}
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

StringBuffer and StringBuilder classes are mutable. You use a single reference and keep inserting data in it, thus you get this result.

Move the declaration and initialization of the StringBuffer inside the loop.

//removed (commented)
//StringBuffer tempWord = new StringBuffer(Word);
for(int i = 0; i<Word.length(); i++) {
    //using StringBuilder rather than StringBuffer since you don't need synchronization at all
    StringBuilder tempWord = new StringBuilder(Word);
    tempWord = tempWord.insert(i, addLetter);
    System.out.println(tempWord);
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Let's look at the iterations:

i goes from 0 to "Java".length() = 4
i = 0 -> insert t in 0 -> Java -> t + Java
i = 1 -> insert t in 1 -> tJava -> t + t + Java
i = 2 -> insert t in 2 -> ttJava -> tt + t + Java
i = 3 -> insert t in 3 -> tttJava -> ttt + t + Java

What you want is in insert t in Java at a different index at each iteration, not at the result of the previous iteration.

Therefore you should not use the result of the previous iteration but re-assign your buffer:

for(int i = 0 ; i < word.length() ; i++) {
    StringBuilder sb = new StringBuilder(word);
    System.out.println(sb.insert(i, "t"));
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

If you insist on using a StringBuffer, you should reassign it to the original word for each iteration. The buffer remembers the insertion.

String addLetter  = "t";
String word = "Java";

for(int i = 0; i<Word.length(); i++) {
    StringBuffer tempWord = new StringBuffer(word);
    tempWord.insert(i, addLetter);
    System.out.println(tempWord);
}

Otherwise see Insert a character in a string at a certain position

Community
  • 1
  • 1
flup
  • 26,937
  • 7
  • 52
  • 74
0

StringBuilder is mutable and will update itself on the call to insert, and thus every loop iteration the instance is updated. To insert the value into the ORIGINAL string at each loop iteration, you should create a new object set to the original String

    for(int i = 0; i<Word.length(); i++)
    {   
        StringBuffer tempWord = new StringBuffer(Word);
        System.out.println(tempWord.insert(i, addLetter));
    }
copeg
  • 8,290
  • 19
  • 28