Reputation: 19
I am making a hangman game. I have two char arrays and I need to check if they are equal.
One of them has letters and underscores: char checkLetter[]
The other one has only letters: char currentWord[]
Eventually, after the user has guessed all the words in the checkLetter[] array it will also consist of only letters. But I need to keep continually checking (in a boolean method) if the array into which they guess and their letters get stored, is the exact same as the word they are trying to guess.
If I could convert both of the arrays into strings then I could check them for equality. I am not experienced, and I don't know how to do this. Any help help would be appreciated!
Upvotes: 1
Views: 344
Reputation:
You really don't need to convert the array, but if you really want to then
try using String word = currentWord.toString()
to convert the char array.
Upvotes: 0
Reputation: 1989
Use the String-constructor:
String str = new String(yourCharArray);
However, that's useless; use Arrays.equals(arr1, arr2)
instead.
Upvotes: 2
Reputation: 46438
you can convert an char array into string using String's overloaded constructor which takes char[] array as argument.
char[] carr ;
String s = new String(carr);
Upvotes: 3
Reputation: 66677
You may use new String(char[] value
)` to create String from char array.
Upvotes: 2
Reputation: 360046
You don't need to convert them to strings at all. Use Arrays.equals()
.
Upvotes: 9