0

So I have written a little Hangman game but I have NO CLUE why it is not working. Normally I would ask a question to solve a problem but I simply do not know what is going on and why I am having this problem. There isn't an error.

Here is all of my code:

 public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
    String mysteryGuess = "hello";
    String userGuess = "";
    int wrongGuesses;


    System.out.println("Hello and welcome to Hang Man!");
    loop:
    for(;;){
        String[] wordAsArray = convertToStringArray(mysteryGuess);
        boolean[] guessed = new boolean[mysteryGuess.length()];
        for (int i = 0; i<wordAsArray.length;i++)
            if(wordAsArray[i] == userGuess)
                guessed[i]=true;
        System.out.println("Word so far:" + visibleWord(wordAsArray,guessed));
        System.out.println("What is your guess?");
        userGuess = sc.next();
        if (guess(userGuess,wordAsArray,guessed) == true)
            System.out.println("Correct");
        else
            System.out.println("Incorrect");
        if (didWin(guessed)==true)
            break loop;
    }
}


//This method creates an array version of the parameter word
//For example, if word contained the data "hello", then this method
//would return {"h", "e", "l", "l", "o"}
//Parameters:   word - a single word
//Returns:      an array containing each letter in word
public static String[] convertToStringArray(String word) {
    String [] pWord = new String [word.length()];
    for (int i = 0; i<pWord.length; i++){
        pWord[i] = word.substring(i,i+1);
    }
    return pWord;

}


//This method determines whether the player has won the game of HangMan
//Parameters:   guessed - array of boolean values
//Returns:      true - if every value in guessed is true
//              false - if at least one value in guessed is false
public static boolean didWin(boolean[] guessed) {
    boolean bGuess = true;
    loop:
    for (int i = 0; i<guessed.length;i++){
        if(guessed[i]==false){
            bGuess = false;
            break loop;
        }


    }
        return bGuess;
}


//This method determines what portion of the hidden word is visible
//For example, if the parameters are as follows:
//     wordAsArray: {"h", "e", "l", "l", "o"}
//     guessed: {true, false, false, false, true}
//Then the method should return "h???o"
//Parameters:   wordAsArray - the individual letters to be guessed
//              guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns:      A string representing how much of the word has been guessed (unguessed letters are represented by ?'s)
public static String visibleWord(String[] wordAsArray, boolean[] guessed) {
    String visibleWord="";
    for(int i = 0; i<wordAsArray.length;i++){
        if (guessed[i] == true)
            wordAsArray[i]=wordAsArray[i];
        if (guessed[i] == false)
            wordAsArray[i]="?";
    }
    for(int i = 0; i<wordAsArray.length;i++){
        visibleWord=visibleWord+wordAsArray[i];
    }
    return visibleWord;
}


//This method checks to see if a player has made a successful guess in the game of Hang Man
//For example, if the parameters are as follows:
//     letter: "e"
//     wordAsArray: {"h", "e", "l", "l", "o"}
//     guessed: {true, false, false, false, true}
//Then the guessed array would be changed to:
//      guessed: {true, true, false, false, true}
//And the method would return false
//Parameters:   letter - the letter that the user has just guessed
//              wordAsArray - an array of individual letters that are to be guessed
//              guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns:  true - if letter matches an unguessed letter in wordAsArray
//          false - otherwise
public static boolean guess(String letter, String[] wordAsArray, boolean[] guessed) {
    boolean appearsAtLeastOnce=false;
    for(int i = 0; i<wordAsArray.length;i++)
        if(letter.equalsIgnoreCase(wordAsArray[i])){
            guessed[i] = true;
            appearsAtLeastOnce=true;
        }
    return appearsAtLeastOnce;



}

}

So when you input your guess even if it is right it will say its incorrect and the "word so far" wont show that you got it correct. May someone please explain to me what is wrong here? I am sure its a simple thing that I am missing but I have been on this for hours. Thank you.

Also, I use "hello" just for testing. I am looking to input a list of various dictionary words.

user3183941
  • 11
  • 1
  • 3

3 Answers3

4

Change

if(wordAsArray[i] == userGuess)

to

if(wordAsArray[i].equals(userGuess)) //Replace .equals with .equalsIgnoreCase for case insensitive compare.

You are comparing string, not object. So == won't work.

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • IMO "You are comparing reference types, not primitive types. So `==` won't work" is more correct than "You are comparing string, not object. So `==` won't work". – The SE I loved is dead Jan 19 '17 at 21:10
2

Your line if(wordAsArray[i] == userGuess) is comparing two objects to see if they are equal. In this case two String objects. This comparison checks to see whether the references of the objects are equal (memory location). Even though the Strings have the same value, they are two different objects and thus have two different memory locations, which will make them unequal.

What you are trying to do, is check to see whether the two objects values are the same. This can be accomplished by using String.equals() method: if(wordAsArray[i].equals(userGuess))

In general, when working with primitive data types (int, float, boolean, char...) the == comparison works just fine. However, when working with objects, you should be using the object's equals() method.

More on primitive types: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Ian Schmitz
  • 320
  • 2
  • 13
0

I have not reviewed your code deeply, but there are some issues about your code (and may be there are some more):

  1. Most important ones is you are defining guessedinsideforloop, so every time the array is initialized to allfalse`.
  2. You are using == for Strings, which won't work, you shall use String.equals() method instead, by the way why didn't you use chars?
  3. java.lang.String has a toCharArray() which returns an char[] representing each character, and because char is primitive you can use == operator on it.
Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69