This program is supposed to work like hangman. My code is inputting extra '?' even after a user makes a correct guess. How do I correct this? I want to use the for loops that already exist and I am not supposed to use arrays as we have not covered that in class to this point. I need to get rid of the extra '?'s that are showing up. Does anyone have any ideas?
Another error that pops up is if someone guesses a letter that has already been guessed, then the program inserts it a second time. I would like to know why, but it is not as important as solving the first problem.
/* This program allows a user to input a phrase and has a second user try to guess the phrase. A '?' will take the place of any letters not guessed while correct guesses will be placed in their correct index location. */ import java.util.*; public class Program07 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); //variables String cPhrase = ""; String guess = ""; //String to be guessed. Input by user. Forced to be lowercase. System.out.println("Please enter a common phrase: "); cPhrase = stdIn.nextLine().toLowerCase(); //Allow a user to make guesses while (guess.length() < 35) { System.out.println('\n'+"Please enter a single character guess: "+ '\n'); guess += stdIn.next().charAt(0); //Handling guesses for (int j=0; j < cPhrase.length(); ++j) { for (int k=0; k < guess.length(); ++k) { if (guess.charAt(k) == cPhrase.charAt(j)) { System.out.print(cPhrase.charAt(j)); } } if (cPhrase.charAt(j) == ' ') { System.out.print(" "); } else { System.out.print("?"); } } } } }