This code is meant to compare the characters in two strings and see if they are the same. It does so by taking the strings, converting them to a char array, sorting them, and then comparing them.
private boolean sameChars(String firstStr, String secondStr)
{
return Arrays.equals(Arrays.sort(firstStr.toCharArray()), Arrays.sort(secondStr.toCharArray()));
}
When I compile this code, it highlights (firstStr.toCharArray()) and says 'void' type not allowed here. What’s causing the error and how would I fix it?
Arrays.sort()doesn’t return the array. You’ll need to store the character array strings to local variables, then call sort on each variable, and then finally compare the two arrays usingArrays.equals():