Hello,
My group and I are currently writing a hangman-like program for our class (I know hangman related questions have been asked a billion times, but bear with me). We are having issues with being able to replace our * with the correct letter (if it is correct). Here is what we are using:

import java.util.Scanner;

public class HangmanMenu
{

	static String word;
	static char guess;
	static int pos;
	static String m;
	static int tries;
	
	public static void main(String[] args)
	{
		Instructions i = new Instructions();
		Scanner keyBd = new Scanner( System.in );
		char selection;

		do{
			System.out.println("\n--------------");
			System.out.println("Main Menu\n");
			System.out.println("1. Play");
			System.out.println("2. Instructions");
			System.out.println("3. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);

			switch (selection)
			{
				case '1':
									
					word = Game.getWord();
					
					m =Game.detNumLet();					
		
//HERE IS WHERE THE PROBLEM IS	
				do
				{
					StringBuffer sb = new StringBuffer(m);

					System.out.println();
					System.out.println(m);
					System.out.println("Guess a letter: ");
					guess = keyBd.next().charAt(0);
										
					pos = word.indexOf(guess);
					
					if (pos>=0)
						sb.setCharAt(pos, guess);
					
					tries++;
				
                            //ignore this for now
					if (m.equalsIgnoreCase(word))
						break;
					
				}while(tries<6);
					
					break;
				case '2':
					i.run();
					break;
				case '3':
					break;
				default :
					System.out.printf("%c\n",7);
					System.out.println("Invalid Selection");
			}

		}
		while( selection != '3');
		
	}
}

Now, from what I understand, using the setCharAt method should take the guess and if it is in the word (pos>=0) it should take the guess and replace it at index pos, but it doesn't. This also doesn't work with double letters (such as the 2 c's in calculus) since it only returns the first index. We'll probably just make our words not have doubles unless anyone has suggestions on how to make this work.

Any help is greatly appreciated!

Hey MokaChick,

Since you have a 'StringBuffer' object your string is now mutable which means you can overwrite whatever position in the string to whatever character you want.

I would do this:

if (pos>=0) {
     sb.charAt(pos) = guess;
}

That should work.

Thank you RunTimeError for your quick reply!

When I try to compile it, it returns this error:

HangmanMenu.java:64: unexpected type
required: variable
found : value
sb.charAt(pos) = guess;

(There's a little ^ pointing to the first parentheses before "pos" in the error too, but for some reason it wont stay there when I post)

Also, I'm not sure I understand what sb.charAt(pos) = guess is trying to do exactly. If I understand, the "charAt" method returns the character at the index you provide. But I'm not sure how that helps our problem. The StringBuffer is using "m" which is just a string of *'s, so if it returns the character at "pos" then wouldn't it just set guess = * instead of replacing the specific * in the string with the correct guess?

I'm sorry I'm having a difficult time wrapping my head around this...

Thank you again for your quick response!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.