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!

2
  • Duplicate of stackoverflow.com/questions/7655127/… Commented Dec 21, 2012 at 16:04
  • 1
    @AaronKurtzhals I disagree. Despite the title, the question is about checking equality of char[]s. Commented Dec 21, 2012 at 16:06

5 Answers 5

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

Would I just write: checkLetter.equals(currentWord) ?
No, read the linked javadocs. Arrays.equals(checkLetter, currentWord).
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);

Comments

2

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

Comments

2

Use the String-constructor:

String str = new String(yourCharArray);

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

1 Comment

Would I just write: checkLetter.equals(currentWord) ?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.