Cannot invoke equals(char) on the primitive type char

后端 未结 2 1194
挽巷
挽巷 2020-12-29 16:58

I\'m new to programming and trying to nut out a basic guessing game, but I have this error. Need some help as I\'ve set \'guess\' to char, then want to compare

相关标签:
2条回答
  • 2020-12-29 17:10

    equals() is a method that is contained in the Object class and passed on through inheritance to every class that is created in java. And since it is a method, it can be invoked only by objects and not primitives.

    You should compare the variable guess like this

    if(guess==wordContainer[j]) {
    

    hope it helps.

    0 讨论(0)
  • 2020-12-29 17:15

    Primitives are compared with ==. If you convert the chars to the wrapper classes Character, then you can use .equals().

    Either change

    1. char guess; to Character guess;

      or

    2. if(guess.equals(wordContainer[j])) to if(guess == wordContainer[j])).

    0 讨论(0)
提交回复
热议问题