3
private String theLetters = "_ _ _ _ _\n";



StringBuilder myName = new StringBuilder(theLetters);    

for(char e : theSecretWord.toLowerCase().toCharArray())
{
    if(e == theUsersGuess.charAt(0))
    {
        int index = theSecretWord.indexOf(e) * 2;
        myName.setCharAt(index, theUsersGuess.charAt(0));
        theLetters = myName.toString();
    }
}

For some reason this will only replace the first occurrence of a letter from the String theSecretWord and not the second, even though this for each loop goes through each character and replaces it in theLetters accordingly. I don't understand why it won't replace more than one occurrence of a letter.

I think it's because the loop stops once it finds a matching letter even though it shouldn't.

Ian Lundberg
  • 1,813
  • 10
  • 31
  • 48
  • Look at the documentation for String: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int) indexOf(char) always gives you the first occurrence – mdewitt Jan 23 '14 at 02:33
  • 2
    Your problem was already solved here: http://stackoverflow.com/questions/5034442/java-indexes-of-all-occurrences-of-character-in-a-string – Lotus Jan 23 '14 at 02:36
  • Yup you're right @mdewitt I completely forgot about that. – Ian Lundberg Jan 23 '14 at 02:36
  • 1
    Use two parameter form of `indexOf` and start searching at previously found position + 1. – PM 77-1 Jan 23 '14 at 02:47
  • What you actually trying to achieve? Is this hangman? – Bohemian Jan 23 '14 at 03:26

4 Answers4

2
private String replaceOccurrence(String text, String replaceFrom, String replaceTo, int occurrenceIndex)
{
    StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile(replaceFrom);
    Matcher m = p.matcher(text);
    int count = 0;
    while (m.find())
    {
        if (count++ == occurrenceIndex - 1)
        {
            m.appendReplacement(sb, replaceTo);
        }
    }
    m.appendTail(sb);
    return sb.toString();
}

For example if you want to replace the second occurrence of "_" with "A", then:

String theLetters = "_ _ _ _ _\n";
String replacedText = replaceOccurrence(theLetters, "_", "A", 2);

Result: _ A _ _ _

Laurel
  • 5,965
  • 14
  • 31
  • 57
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
1

Another Way is

 String string1= "Hello";
 int first=string1.indexOf('l');
 String newstr= string1.substring(0, first+1);
 String newstr2= string1.substring(first+1, string1.length()).replaceFirst("l", "k");
 System.out.println(newstr+newstr2);
0

I think this is the code you're looking for,

 String word, letter;
        word = "test";
        letter = "t";
        int i = 0;

        i = word.indexOf(letter);

        while (i > -1) {
            // store i in arrayList
            i = word.indexOf(letter, i + 1);}
java-love
  • 516
  • 1
  • 8
  • 23
0

By using StringBuilder you can achieve this by using below code

    String string1= "Hello";
    int first=string1.indexOf('l',string1.indexOf('l')+1);
    StringBuilder SB = new StringBuilder(string1); 
    SB.setCharAt(first, 'k'); 
    System.out.println(SB);