I'm having a problem with this code, specifically at the "if (guess.indexOf(letter) >= 0))" part, where I get the error: "The method indexOf(String) in the type StringBuilder is not applicable for the arguments (char)"
I highlight for suggestions and it just creates a loop, so it's just frustrating.
The other thing, I seem to have a problem doing (and always have for some reason), is that I know a do/while part would help, but I keep putting it in different places, but not working. Any suggestions? I'm not wanting direct answers, but some hints would be helpful.
import java.util.Scanner; public class HangmanClass { public static void main(String[] args) { // TODO Auto-generated method stub String [] words= {"write", "that", "program", "fusion"}; Scanner input= new Scanner(System.in); char newGame; { int index= (int)(Math.random() * words.length); String word= words[index]; StringBuilder guess= new StringBuilder(); for (int i=0; i < word.length(); i++){ guess.append('*'); } int CorrectGuesses=0; int Misses=0; while (CorrectGuesses < word.length()){ System.out.println("(Guess) Enter a letter in word " +guess+ " > "); String s= input.nextLine(); char letter= s.charAt(0); if (guess.indexOf(letter) >= 0){ System.out.println("\t" +letter+ " is already in the word");} else if (word.indexOf(letter) < 0) { System.out.println("\t" +letter+ " is not in the word"); Misses++;} else { int h= word.indexOf(letter); while (h >= 0) { guess.setCharAt(h, letter); CorrectGuesses++; h= word.indexOf(letter, h+1); } } } System.out.println("The word is " +word+ ". You missed " +Misses+ " time(s)."); System.out.println("Do you want to guess another word? Enter y or n>"); } } }