0
val args = "To now was far back saw the *$# giant planet itself, het a won"

Find and sort distinct anagram pairs from "args":

now won

was saw 

the het

First I clean up the args and put them in an array.

val argsArray = args.replaceAll("[^a-zA-Z0-9\\s]", "").toLowerCase.split(" ").distinct.sorted 
argsArray: Array[String] = Array("", a, back, far, giant, het, itself, now, planet, saw, the, to, was, won)

My idea is to reduce each word to an array of char, then sort, then compare. But I get stuck because the following returns the wrong data type ---- String = [C@2736f24a

for (i <- 0 until argsArray.length - 1){
    val j = i + 1
    if(argsArray(i).toCharArray.sorted == argsArray(j).toCharArray.sorted) {
      println(argsArray(i).toCharArray + " " + argsArray(j).toCharArray)
    }
}

I assume there are better ways to solve this, but what I really want to learn is how to deal with this data type problem, so please help me solve that and then I will refactor later. Thank you.

6
  • 1
    "the following returns the wrong data type ---- String = [C@2736f24a" ... No, it doesn't. The code fragment you posted returns a Unit. Show the actual error message you are getting along with the code that generates it. Also, you need another loop (the way it is now, you are only comparing each word to the next in the list, not to every other word), and might want to sort all words in list beforehand, so that you don't have to sort them over and over before every comparison. Commented Mar 28, 2016 at 16:35
  • java.lang.ArrayIndexOutOfBoundsException: 13 at Notebook$$anonfun$1.apply$mcVI$sp(<console>:42) at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:141) Commented Mar 28, 2016 at 17:13
  • yup. What did you expect would happen when i is at the last element of the array? What would argsArray(j) be then? Commented Mar 28, 2016 at 17:15
  • Yes, I knew that problem was there, but I am trying to build toward a solution. So the first thing I would like to solve is how to pass around the correct data types. --- OK now I am thinking about your question. Commented Mar 28, 2016 at 17:21
  • for(i <- 0 until argsArray.length - 1){ val j = i + 1 if(argsArray(i).toCharArray.sorted == argsArray(j).toCharArray.sorted) { println(argsArray(i).toCharArray + " " + argsArray(j).toCharArray) } } Commented Mar 28, 2016 at 17:28

1 Answer 1

1

[C@<whatever> is just how Array[Char] is converted to String on JVM. Remove calls to toCharArray from println and it'll print the strings you want. The second error, with the current code in the question, is the equality check: == on arrays checks that they are the same object, and since sorted will always create a new array, the left and right sides are always different objects even if they have the same elements.

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

3 Comments

Thank you Alexey. This is what I am really wanting to ask: argsArray(9).toCharArray.sorted ---->>> Array[Char] = Array(a, s, w) and argsArray(12).toCharArray.sorted ------>>> Array[Char] = Array(a, s, w) ................... so how do I do a comparison?
"This is what I am really wanting to ask" Then ask it (as a separate question)
This is getting what I was after: .............. argsArray(9).toCharArray.sorted.deep == argsArray(12).toCharArray.sorted.deep

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.