Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: For Loop and String Problem - Console Hangman Game

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question For Loop and String Problem - Console Hangman Game

    I am doing my Hangman Project and need assistance. I am doing the first part, have managed to hide the word using , but now the problem I am facing is. After I first guess I need it to replace with appropriate letter. But with the code below here is my output. The word in this example is "hello"

    Welcome to Hangman... v1.0 - Designed Ashley Bwanya
    This is the word you have to guess:

    * * * * *

    Enter your first guess
    h
    h* *

    import java.util.Scanner;
    public class Hangman
    {
    public static void main(String[] args)
    {
    int attempts = 6;
    String secret;
    String displaySecret = "";

    Scanner scn=new Scanner(System.in);

    Game myWord = new Game();

    secret = myWord.getWord();

    for (int i = 0; i < secret.length(); i++)
    {
    displaySecret += " * ";
    }
    System.out.println("Welcome to Hangman... v1.0 - Designed Ashley Bwanya");
    System.out.println("This is the word you have to guess: \n");
    System.out.println(displaySecret+ "\n");

    System.out.println("Enter your first guess");
    char firstGuess = scn.nextLine().charAt(0);

    int position = secret.indexOf(firstGuess);

    String newDisplaySecret = "";

    for (int i = 0; i < secret.length(); i++)
    if (i == position)
    newDisplaySecret += secret.charAt(i); //newly guessed character
    else
    newDisplaySecret += displaySecret.charAt(i); //old state

    displaySecret = new String(newDisplaySecret);

    System.out.println(newDisplaySecret);

    }
    }


  2. #2
    Junior Member JavaManNoob's Avatar
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loop and String Problem - Console Hangman Game

    I don't want to ruin the challenge for you but if you would like you can take a look at my java hangman example... http://www.javaprogrammingforums.com...a-hangman.html however keep in mind that I am a beginner here as well but I did manage to get that part of my program to work just fine using a StringBuilder. This allowed me to modify a string where as a String is immutable.

    String word = "";
    for(int i = 0; i < secret.length(); i++){ // display dashes for length of word
    word += "-";
    }
    StringBuilder guessedWord = new StringBuilder(word);
    System.out.println(guessedWord); // print number of dashes for word
    for(int i = 0; i < guessedWord.length(); i++){ // check for letter in word
    if(guess == secret.charAt(i)){
    guessedWord.setCharAt(i, guess);
    }
    }//end for

Similar Threads

  1. Complete hangman game: Good example for seeing how everything goes together
    By ShadeDarkan in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: July 9th, 2012, 06:31 AM
  2. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  3. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  4. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM

Tags for this Thread