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);
}