3

I have an Iterator[String] that I want to convert to a String in Scala. I would expect the following to work, but I get no output. What am I doing wrong?

val it = Iterator("a", "number", "of", "words")
val combined = "";

while (it.hasNext){
   combined = combined + it.next()
}
println(combined)
1
  • 4
    I hope it does not even compile. :) You try to reassign a val. Commented Dec 15, 2014 at 13:53

1 Answer 1

13

Your code can't even compile, since you're reassigning a val

it.mkString

will do what you're after

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

1 Comment

I think you can even leave out the (""). It should be the default.

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.