shayon shayon - 2 months ago 6
Scala Question

Scala : Print List of List into a file : each nested list is a new line

Pretty New to Scala.
Trying this

val ListNew=List(List("this", "is", "it?"), List("yeah", "what", "else", "do","you","expect"))


into a File / Screen , like this:

this is it? \n
yeah what else do you expect


I did manage it with the following code

var ctr =0 ;
ListNew.map(k=> for(rec <-k ) {ctr=ctr+1 ;if (ctr == k.length){print(rec +"\n " );ctr =0; } else {print(rec+" ")} })


But I dont want counters : Is there a way to get a print "\n" before two foreach .I tried Iterators , could not get them right

Answer
println(ListNew.map(_.mkString(" ")).mkString("\n"))
Comments