Results 1 to 3 of 3

Thread: hangman

  1. #1
    javaMike is offline Member
    Join Date
    Nov 2007
    Rep Power

    Default hangman

    i'm trying to create a simple version of hangman w/out using array/arraylist.

    my code is attached works but would like to be able to change value of a string character position w/out having to use a "dummy" string (answer).

    my code ask user to enter a word.

    then draws blank for each char

    when user enters a letter use a loop to find it in the string but...
    would like to be able to do something like
    word (i) = guess; which obviously you cannot do.

    is there a way to directly overwrite the char stored in a certain postion in a string?
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    Consider using a StringBuilder which is mutable. Ie you can do things like:

    Java Code:
    StringBuilder foo = new StringBuilder("foo");
    foo.setCharAt(1, '_');

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    This hangman question is asked so often that I decided to implement it myself and post it as a complete spoonfeeding spoiler; all the work is done by the regular expression engine (no ArrayLists, no loops, no arrays, no character comparisons, no nothing):

    Java Code:
    public class HangMan {
    	private BufferedReader br= new BufferedReader(new InputStreamReader(;
    	private String correct, used, word;
    	private int turns;
    	private void setup(String w, int turns) {
    		correct= "-"; used= ""; word= w;
    		this.turns= turns;
    	private String word(String prompt) {
    		String word= this.word.replaceAll("[^"+correct+"]", "-");
    		System.out.println(prompt+": "+word);
    		return word;
    	private boolean wins(String word) {
    		boolean wins= word.indexOf('-') < 0;
    		if (wins)
    			System.out.println("You guessed the word");
    		return wins;
    	private String guess(char x) {
    		if (used.indexOf(x) >= 0) 
    			return word("Character '"+x+"' already used");
    		used+= x;
    		if (word.indexOf(x) < 0) {
    			return word("Character '"+x+"' is incorrect");
    		correct+= x;
    		return word("Character '"+x+"' is correct");
    	private char input() {
    		String s= "";
    		try {
    			for (; (s= s.trim()).length() == 0; s= br.readLine()) 
    				System.out.print("Guess a character: ");
    		} catch (IOException ioe) { System.exit(1); }
    		return s.charAt(0);
    	public boolean play(String word) {
    		for (setup(word, word.length());; System.out.println("turns: "+turns)) 
    			if (wins(guess(input()))) 
    				return true;
    			else if (turns == 0) {
    				System.out.println("You lose");
    				return false;
    	public static void main(String[] args) {
    		new HangMan().play("defenestration");
    kind regards,


Similar Threads

  1. Hangman GUI help
    By kurt in forum New To Java
    Replies: 5
    Last Post: 05-22-2009, 11:22 AM
  2. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  3. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:42 AM
  4. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 03:30 AM
  5. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts