Hi,
here is my program:
package practice; import java.util.*; public class New2 { public static void main(String[] args) { // TODO Auto-generated method stub String [] words= {"write", "that", "program", "fusion"}; Scanner in= 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= in.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>"); } } }
I have one error: the line >> if (guess.indexOf(letter) >= 0){
This line is preventing my program from running
The error: The method indexOf(String) in the type StringBuilder is not applicable for the arguements (char).