Reputation: 64
How to read integer list from command line ?
Like "1 2 3 4 5\n"
The excepted type in return is List[Int]
Upvotes: 1
Views: 2239
Reputation: 15345
Here is a solution where you can filter to escape the \n
.
val input = "1 2 3 4 5\n"
val myList = input.filter(_!='\n').split(' ').map(_.toInt).toList
Upvotes: 0
Reputation: 12852
Save this Scala script
val xs: List[Int] = args(0).split(' ').toList.map(_.toInt)
println(xs)
as split.scala
and run it as (on Windows)
scala.bat split.scala "1 2 3 4 5"
The output is
List(1, 2, 3, 4, 5)
Upvotes: 7