hi. i am having trouble with incrementing a counter for this string
i have a string, eg. String s = "computer" i have a user input that guesses if the character entered is in the string s. i want to count how many wrong guesses there are, each time it is entered.
i did something like this, which only gives the TOTAL number of wrong guesses:
for (int i=0; i < s.length(); i++) { if( character != s.substring (i, i+1) { numberGuess++; } }
however this doesnt work because it doesnt stop and return that number, but it just adds on continously for the entire length of the word. does anyone have suggestions?
Well, you're not correctly testing to see if you have a match. Also, to enchance your for loop .. never have a method as the conditional ie. s.length(). This method will be called each time through the loop.
Try something like this. You'll have to modify it to test.
public void checkForMatch(String s){ boolean match = false;
int length = s.length();
String letter; String guess = "a";
for(int i=0; i<length && match == false; i++){ letter = s.substring(i, i+1);
if(letter.equalsIgnoreCase(guess)){ match = true; }