Selvart
Selvart

Reputation: 19

Converting char array into a string

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

Answers (5)

user2675678
user2675678

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

looper
looper

Reputation: 1989

Use the String-constructor:

String str = new String(yourCharArray);

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

Upvotes: 2

PermGenError
PermGenError

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

kosa
kosa

Reputation: 66677

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

Upvotes: 2

Matt Ball
Matt Ball

Reputation: 360046

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

Upvotes: 9

Related Questions