Hang Man

I am attempting to develop a hang man game but, I can't get the String Buffer to work in seperating my characters. When prompting input from the user(The word to be guessed) how do I put the individual characters of the inputted word into an array. Do I have to use the String Buffer at all?

Any hints to develop this hang man game please Respond!!!

[370 byte] By [JLam] at [2007-9-26 1:15:02]
# 1

Hi,

I'm not sure to nderstand ur problem.

Just get the word to be guessed an create a new StringBuffer with it.

Then to check characters enter to guess the word use the chatAt(int position) method in StringBuffer.

Here a sample code that ask for the word to be guessed then a character and check if it exists in the secret word before showing where the letter appears

char c;//char enter to guess the word

StringBuffer word; // word to be guess

StringBuffer found;//character found

...

BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the word to be guessed");

word=new StringBuffer(reader.readLine());

for (int i=0;i<word.length();i++)

found.append('_'); // use a '' word to begin

...

//add a loop to ask for a character until the word is guessed or the game ends

//check a letter

System.out.println("enter a character");

c=reader.readLine().charAt(0);

for(int i=0;i<word.length();i++){

if (c==word.charAt(i)){

found.charAt(i)=c;

}

}

System.out.println(found);

//end loop

Hope this helps

>

gentyt at 2007-6-29 0:40:53 > top of Java-index,Archived Forums,Java Programming...
# 2
Thanks...:)
JLam at 2007-6-29 0:40:53 > top of Java-index,Archived Forums,Java Programming...