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 16 of 16

Thread: Hangman multiple letters

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman multiple letters

    Hi everyone,
    I've been struggling on a hangman game the past few hours and I got almost everything down except the multiple letters part.
    Like if the word is "camera" but whenever the user inputs "a" it only shows "-a----" instead of "-a---a"

    this is my code so far:
    if (secretWord.indexOf(letterGuess) >= 0) {
    updatedWord = wordSoFar.substring(0,
    secretWord.indexOf(letterGuess));
    updatedWord += letterGuess;
    updatedWord += wordSoFar.substring(
    secretWord.indexOf(letterGuess) + 1,
    wordSoFar.length());
    wordSoFar = updatedWord;
    }

    and i know indexOf() only returns the first occuring letter, so I'm wondering how to return EVERY occuring letter.
    and is there a way to do this other than char and arrays cause I was googling online and it's always done with char and array but I haven't learned that yet.

    Any help is appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    indexOf() only returns the first occuring letter,
    There is another version of the indexOf() method that will start its search at another column besides column 0.
    Use that version with the column past where the last match was found. If found at 8 start next search at 9
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    Sorry, I'm kind of confused.
    Is it something like this:?
    for (int i = 0; i <= secretWord.length(); i++){
    if (secretWord.indexOf(letterGuess,i)....

    cause I tried that and it didn't work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    You need to save the last value returned by indexOf to use in the next search.
    If you find the first "a" at 1, then start the next search for an "a" at 2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    sorry i'm still stuck on what to do. can you give me an example?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    Something like this:
    find first one and save the column: int ix = str.indexOf("a")
    look for the next one starting at that saved column+1
    int ix2 = str.indexOf("a", ix+1);
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    i've tried every loop method and none seem to work
    i did the do-while loop and for loop with int i and i += i; but the multiple letters didn't come out.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hangman multiple letters

    Use a single loop to iterate over each char in secretWord. If the char matches the guess letter then change the dash in the updatedWord to the guess letter at the same index.

    Or you could use replaceAll.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    but how do i put that into my if-else statement? and can i loop it or do i have to make a different int for every time?

    and I'm using string, not char. i don't know how to use char yet ;(

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hangman multiple letters

    loop each char in secretWord {
        if char at index equals guess {
            change dash to guess in updatedWord
        }
    }
    Improving the world one idiot at a time!

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    Since the code needs to continue looking for a letter until it is not found, code would need a loop.
    You could use a 0 for the starting column for the indexOf() search for the first search.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    um sorry to sound stupid but what's a column?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    I used the word column as if the String were a row made up of columns. The first column is the first position in the String at index=0.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman multiple letters

    okay i can't do it. i've been at it the entire day and i still can't figure out how to put everything together
    oh well, thanks for helping

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,612
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Hangman multiple letters

    Maybe tomorrow the light will come on.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hangman multiple letters

    Quote Originally Posted by JavaN3wbie View Post
    okay i can't do it. i've been at it the entire day and i still can't figure out how to put everything together
    oh well, thanks for helping
    Even with the pseudocode I provided above? All you have to do is convert it to actual code using calls to methods in the String class.
    Improving the world one idiot at a time!

Similar Threads

  1. Hangman java question double letters
    By sparky971 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2012, 01:01 PM
  2. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  3. Replies: 6
    Last Post: December 9th, 2011, 04:53 PM
  4. Problem with digits/letters
    By te09hn8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2011, 07:30 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM