0

Hi am new in scala basic

i have a doubt

This is about separator in scala

Here the string separated by comma and the string will separated and print it down

i don'nt know this code is possible for that

object SeparatorDemo {
  def main(args: Array[String]) {
    var stmt:String=("a,number,of,words")
    var p=stmt.split(",")
    var x=p.length
    for(i <-0 to x){
      println(p)
     }
   }
}

output:

[Ljava.lang.String;@142c842c
[Ljava.lang.String;@142c842c
[Ljava.lang.String;@142c842c
[Ljava.lang.String;@142c842c
[Ljava.lang.String;@142c842c

with regards Mila

1
  • 1
    Not sure who and why was it negative voted. Anyone who is new to scala is welcome to ask questions. If question was not having correct details instead of marking -1 better Edit it and mention in comment :-) Commented Jan 19, 2020 at 10:33

1 Answer 1

3

You're printing out the array p at each iteration. You want to print out the contents of the array that contains the strings. I simplified it to this version.

object SeparatorDemo {
  def main(args: Array[String]) {
    var stmt:String=("a,number,of,words")
    var words=stmt.split(",")
    for(word <- words){
      println(word)
    }
  }
}

Even more concise is to chain the splitting and the iteration into one statement: stmt.split(",").foreach(println(_))

Either way gives this output:

scala> SeparatorDemo.main(Array())
a
number
of
words
Sign up to request clarification or add additional context in comments.

1 Comment

thanx alot.................Brian.. it's my college assignments and my friends are really tough with this.. u really help me...

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.