Search:

Type: Posts; User: Norm

Search: Search took 0.22 seconds.

  1. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    In the for loop, the posted code will always set hanged to false. There is no need to set hanged to false if that was its initial value because its value won't change unless the code sets it to true...
  2. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    Look at and think about how you are using the hanged variable.
    The new variable will be used to detect when: "the user guesses the wrong character"
    or a better way to look at it: when the user...
  3. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    What are you trying to count?
    The loop compares a single user input against all the letters in word.
    Do you want to count all the letters in the word that don't match the user's input?

    Should...
  4. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    What do these two lines of code do? Why are they both there?

    dash[b] = guess.charAt(0);

    dash[word.indexOf(guess.charAt(0))] = check;


    Why does the code set...
  5. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    When should the hanged variable be set to true?
    The code looks like it sets hanged true when ONE character matches.
    Shouldn't all the characters have been matched before hanged is set true?
    ...
  6. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    Post the code on line 64.

    Try debugging the code on line 64 by adding a println just before it that prints out the length of the String, the value of the String and the value of the index. The...
  7. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    Did you look at line 64 to see why the code is using an index value that is past the end of the String?
  8. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    Does that mean you have solved your problem?
  9. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    That is NOT a compiler error. That error is when the code is executed.
    The code at line 64 uses an index value that is past the end of the String. A String of length 2 does not have a char at index...
  10. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    if dashArray is a char array, then you can use the == operator to compare an element to a char:

    if(dashArray[ix] == someChar) { ...

    When you get compiler errors, copy the full text of the error...
  11. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    A String and a char should never return true with the equals() method.
    Can you make dashArray a char array
    or use charAt(0) with the String in dashArray to get a char value to compare with ==?
  12. Replies
    24
    Views
    1,294

    Re: Java hangman help with comparing array to String

    What are the values of dashArray[b] and word.charAt(b) when it doesn't work?
    One possible problem is that dishArray holds String values and charAt() returns a char.

    Also posted at: Hangman game...
Results 1 to 12 of 12