0

I am trying to fetch the nbr from a string given. I have used regex object. But Its giving me address value. Please explain why toArray is not giving the result.

ackage Phase1Training
object ScalaRegex {
  def main(args: Array[String]): Unit = {
        val numPattern = "[0-9]+".r
        val address ="123 washington Road 102"
        var zip = numPattern.findFirstIn(address)

        println(zip)
        **var matches = numPattern.findAllMatchIn(address).toArray** //Why this is giving the object address.
        println (matches)
        matches.foreach(println)  //This is perfectly working.

Some(123) [Lscala.util.matching.Regex$Match;@30097f5f 123 102

1 Answer 1

2

You are using println on an Array. The println method on many object is not meant to be displayed, and is basically unimplemented. Try mkString on collections, which accepts an optional string delimiter:

myArray.mkString // e.g. "el1el2el3"
myArray.mkString(",") // e.g. "el1,el2,el2"

And then print that.

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

1 Comment

It is implemented in some sense, it's just that default implementation that comes from the very top of class hierarchy (Object) is not what you would expect to see normally

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.