84256

Type mismatch, cannot convert from void to char[]

Question:

I'm testing if 2 strings are anagrams. I figured that if I make them arrays then sort them, and they turn out to be equal, they are anagrams. I'm having a type mismatch compiler error though, and I cannot figure out why.

package local.random; import java.util.Arrays; public class Anagram { public static void main(String[] args) { String s1 = "go hang a salami"; String s2 = "im a lasagna hog"; System.out.println(s1); System.out.println(s2); System.out.println(IsAnagram(s1,s2)); } public static boolean IsAnagram (String s1, String s2) { char[] s1Char = s1.toCharArray(); char[] s2Char = s2.toCharArray(); char[] s1Sorted = Arrays.sort(s1Char);//Type mismatch: cannot convert from void to char[] char[] s2Sorted = Arrays.sort(s2Char); System.out.println("The first string sorted alphabetically is :" + s1.toString()); System.out.println("The first string sorted alphabetically is :" + s2.toString()); if (s1Sorted.equals(s2Sorted)){ return true; } else return false; } public String toString() { return "Anagram []"; } }

Answer1:

Arrays.sort() does not return an array. Instead, it modifies the contents of the array, just remove the char[] s1Sorted = and char[] s2Sorted = parts. Also, use Arrays.equals() to compare arrays, and your System.out.println() statements print the strings, not the array. To convert an array to a string, do Arrays.deepToString, or if you just want a string representation of the char array, do new String(param), where param is the char array.

Answer2:

Here is the corrected code . sort takes Array as reference it doesnt return anything. Array comparison can be done by Array.equals

public class Anagram { public static void main(String[] args) { String s1 = "go hang a salami"; String s2 = "im a lasagna hog"; System.out.println(s1); System.out.println(s2); System.out.println(IsAnagram(s1, s2)); } public static boolean IsAnagram(String s1, String s2) { char[] s1Char = s1.toCharArray(); char[] s2Char = s2.toCharArray(); Arrays.sort(s1Char);//Type mismatch: cannot convert from void to char[] Arrays.sort(s2Char); //need to construct a new String to print. System.out.println("The first string sorted alphabetically is :" + new String(s1Char)); System.out.println("The first string sorted alphabetically is :" + new String(s2Char)); return Arrays.equals(s1Char, s2Char); } @Override public String toString() { return "Anagram []"; }

Answer3:

Just put these two lines :

Arrays.sort(s1Char); Arrays.sort(s2Char);

It will automatically sort these char arrays. No need to re assign it.

Answer4:

Arrays.sort is a void method because it works by changing the array you passed to it.

Also, sorting the array you get from toCharArray will not sort the characters in the original string. String is immutable (cannot be changed), so toCharArray returns a <em>new</em> array.

The correct way to sort the characters of a string is this.

String string = "SPOILAGE"; char[] arr = string.toCharArray(); Arrays.sort(arr); String word = new String(arr); System.out.println(word);

Answer5:

Arrays.sort(char[]) is a method that returns void. Change your method to:

public static boolean IsAnagram (String s1, String s2) { char[] s1Char = s1.toCharArray(); char[] s2Char = s2.toCharArray(); Arrays.sort(s1Char); Arrays.sort(s2Char); char[] s1Sorted = s1Char; char[] s2Sorted = s2Char; System.out.println("The first string sorted alphabetically is :" + s1.toString()); System.out.println("The first string sorted alphabetically is :" + s2.toString()); if (s1Sorted.equals(s2Sorted)){ return true; } else return false; }

Recommend

  • How to pass data from one View Controller to another in a Storyboard when going back?
  • Error getting Images into Listview from server using Json
  • Changing row and column names on matrices to numbers
  • Exception Error in mininet
  • TypeError: Cannot find function getDataRange in object
  • MIPS Constructing Loops
  • Deserialize JSON into a generic map, forcing all JSON floats into Decimal or String, in Jackson
  • Parsing LIUM Speaker Diarization Output
  • Fitting multimodal distrubtions
  • SkImageDecoder::factory returned null
  • Frama-C anagram function behavior verification
  • memcpy of a part of a struct
  • How to use discontinuous range in SUMIF
  • Why not allowing std::string initialization from array of chars?
  • Stop a find-in-file search in Visual Studio 2013 which is taking forever
  • Using a case statement in a check constraint
  • Vim syntax highlighting for multiline fortran openmp directives
  • Automatic series in gnuplot?
  • D: how to extract data from archive?
  • Matlab FFT and FFTW
  • Login to a website using python
  • About multiple inheritance and ambiguity
  • PHP: Convert single-quoted string into double-quoted
  • How do you download a image/png data URI in Edge?
  • Problems using HOGDescriptor
  • Why doesnt this Java loop in a thread work?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Android full screen on only one activity?
  • Resize panoramic image to fixed size
  • How to model a transition system with SPIN
  • Weird JavaScript statement, what does it mean?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?