scalaioiteratorstdinscala-2.10

Continue reading stdin until "END" in Scala?


How do I continue reading stdin until "END" string reached, in Scala?

Here's what I've tried:

val text = Iterator.continually(Console.readLine).takeWhile(_ != "END").toString

Solution

  • You should use mkString instead of toString here:

    val text = Iterator.
        continually(Console.readLine).
        takeWhile(_ != "END").
        mkString("\n")
    

    mkString on collection aggregates all elements in a string using optional separator.