Converting char array into a string - java

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!

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

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);

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

Use the String-constructor:
String str = new String(yourCharArray);
However, that's useless; use Arrays.equals(arr1, arr2) instead.

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.

Related

How to convert Binary Srtring into an integer array in java?

I am reading a file as byte and converting them into binary string using
String.format("%8s", Integer.toBinaryString(resultByteArray[i] & 0xFF)).replace(' ', '0')
After that, using StringBuilder I have combined all strings into one string. Now I want to convert this string into an array like [1,0,1,0....].
Is there any way to do so?
Since you've already compiled it into one big String, you can immediately convert it to a char array. There's a method called .toCharArray() that you can use to return a char array.
Here's an example of this:
http://www.javatpoint.com/java-string-tochararray
Additionally, if you want to convert it to a different type of array, you could iterate through and add it to an array that has the same size as the char array.
Best of luck!

How can I compare a string and a char array in Java?

In my program I'm trying to compare my char array asterixA[] to a String word in an if condition like:
if (word.equals(asterixA))
but it's giving me an error. Is there any other way I can compare them?
you have to convert the character array into String or String to char array and then do the comparision.
if (word.equals(new String(asterixA)))
or
if(Arrays.equals(word.toCharArray(), asterixA))
BTW. if is a conditional statement not a loop
You seem to be taking the "A String is an array of chars" line too literal. String's equals method states that
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
It all depends of the circumstances, but generally you compare two objects of the same type or two objects belonging to the same hierarchy (sharing a common superclass).
In this case a String is not a char[], but Java provides mechanisms to go from one to the other, either by doing a String -> char[] transformation with String#toCharArray() or a char[] -> String transformation by passing the char[] as a parameter to String's constructor.
This way you can compare both objects after either turning your String into a char[] or vice-versa.
You can compare the arrays:
if (Arrays.equals(asterixA, word.toCharArray()) {}
do as follows: if (word.equals(new String(asterixA))) { ... }

Reading one character at a time from a large string

I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?
I do not think there is a faster way. But please correct me!
A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.

setting a string's char' by index

I have an ArrayList of strings, and I want to randomly change a string's (random) char' by index.
ArrayListName.get(i).charAt(j)
exists. What's the equiv' for a setter?
Strings are immutable so they can't change. You can use a StringBuffer or Character Array. StringBuffer has a setCharAt method that takes an index and character.
If you want to use a String you'll have to create a new String with the character changed and replace the old String.
String is immutable. If you want to change the specific location's string, then you would want to use String.replaceAll() and capture the results in your ArrayList.

Explanation of this line of code?

Can you explain what is happening in this line of code? Specially what is args[0].tocharArray ?
char[] password = args[0].toCharArray();
char[] is your datatype. "char" is a single 16 bit character, and char[] is a character array.
args[0] is the first argument that's passed to the program.
.toCharArray(); converts that argument to a character array.
This line of code is basically taking an argument, turning it into a character array, and storing it in "password" which is a character array.
It's converting the first argument of a Java program—passed as a String[] to the main method—to a character array.
Most password-oriented APIs use char[] so that after calling the method, the caller can "zero-ize" the array, effectively erasing the password from memory. Since Java String instances are immutable, they can't be zero-ized. However, in practice, it's hard to get user-input without using a String. All web frameworks will convert passwords submitted in a web request to a String. Swing password widgets and Java 6's Console class will input char[], however.
args[0] is presumably a String array. Thus it is a call to the method String.toCharArray() which converts a String to an array of chars.
EDIT: Corrected my answer after comment.
It converts the first item of the args array (presumably, the first command line argument passed to the main method, which is of string type) to an equivalent array of chars (an array containing all the chars that build up the string).
args is an array.
The type of the array contains a function called toCharArray which returns an array of characters. NOTE: args is most likely an array of strings
So it takes the string in args[0] and creates an array of characters which represents that string.
args[0] - representing a string
toCharArray() - convert this string to char array
I thought this, toCharArray(), might help.

Categories