I am trying to replace letters in a string at specific locations. I am aware that there are many questions such as this already however I am still getting into trouble.
example: hiddenWord = "----" from my loop I find that at location 1 and 3 I would like to replace "-" with "a". So that hiddenWord now = "-a-a".
Main snip:
btnA = new JButton("A");
btnA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
count += 1;
lblTries.setText(count + " Tries");
int i;
String newName="";
if (wordList[num].indexOf('a') > 0){
System.out.print("Has A: ");
for (i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
//System.out.print(i + " ,");
newName = hiddenWord.substring(0,i)+'a'+hiddenWord.substring(5);
}
}
System.out.println(newName);
}
});
Please let me know if there are any other conventions that I should be doing differently..as you can tell I am very new to this.
Edit:
someone_somewhere has helped me see my error. My new code looks as followed
if (wordList[num].indexOf('a') >= 0){
for (int i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
hiddenWord = putCharAtPlaces(hiddenWord,'a',new int[]{i});
lblWordDisplay.setText(hiddenWord);
System.out.println(i);
}