1

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!

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Selvart
  • 19
  • 1
  • 1
  • 2

5 Answers5

9

You don't need to convert them to strings at all. Use Arrays.equals().

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
3

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);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

You may use new String(char[] value)` to create String from char array.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
kosa
  • 65,990
  • 13
  • 130
  • 167
2

Use the String-constructor:

String str = new String(yourCharArray);

However, that's useless; use Arrays.equals(arr1, arr2) instead.

looper
  • 1,929
  • 23
  • 42
0

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.