0

I am making a hang-man sort of a game. Currently, I imported a .txt dictionary. I am trying to compare a guess array to the word array. Both arrays are char data types. However, using blueJ I can tell that both arrays are identical but the if(workWord.equals(realWord)) is not executing.

while(togLoop) {
        System.out.print("Please enter a guess! ");
        char input = keyboard.next().charAt(0);

        for(int i = 0; i < wordGuess.length();i++) {
            if(realWord[i] == input){
                correct += 1;
                System.out.println("It is the " + (i + 1) + " letter");
                workWord[i] = input;

            }
        }
        if(workWord.equals(realWord)) {
            System.out.println("Congrats! You got it right!");
            togLoop = false;
        }
    }

~This is what blueJ is showing ~ Image shows they are similar

Jack Sullivan
  • 57
  • 1
  • 5

1 Answers1

4

Use Arrays.equals to compare arrays for equality.

Arrays inherit equals from Object, so two distinct array instances are never equal, even when their contents are the same.

char[] first = {'a'};
char[] second = {'a'};

System.out.println(first == second); // false
System.out.println(first.equals(second)); // false
System.out.println(Arrays.equals(first, second)); // true
Andy Turner
  • 137,514
  • 11
  • 162
  • 243