Galaad
Galaad

Reputation: 64

How to read int list from command line in Scala

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

Answers (2)

Christopher Chiche
Christopher Chiche

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

Malte Schwerhoff
Malte Schwerhoff

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

Related Questions