Hi
I am comparing a letter in a string (random_word) against an input character (c).
trouble is, my for loop only finds the first occurence of a letter in random_word. If there are more than one of any letter it fails to find it. I thought I would remove a letter from the String each time the for loop is traversed to remove this problem.
The only trouble is the replace() method for String doesnt work correctly. The class compiles but never replaces the letter in the String. Very odd. Can any experienced folks see a glaring error in my code.
Also would there be a better way of constructing the for loop to find each occurence of the letter in random_word?
Many Thanks in advance
javaStorm
Here is the code I wrote
Code:/** *check for letter in word **/ public void letterInWord (ArrayList current_word, String random_word, int word_length, String c) { for(int i=0; i < word_length; i++) //start of FOR loop to check letter exists { System.out.println (random_word.indexOf(c));//DEBUG show value to terminal if(random_word.indexOf(c) == i) { System.out.println ("Success"); String replace_char; replace_char = "#"; random_word.replace(c ,replace_char); current_word.remove(i); current_word.add(i,c); printCurrentWord(current_word, random_word, word_length); enterLetter(current_word, random_word, word_length); }//end of if loop } //end of for loop


Reply With Quote


Bookmarks