Scala read first element of line separately -
i have following input on line:
- number of elements in array
- elements of array
for instance
3 1 2 3
or
5 10 20 30 40 50
where 3
, 5
number of elements.
i read in scala(first number of elements, elements).
i tried doing this:
val n = readint val = readline.split(" ").map(_.toint)
however, doesn't read n
correctly.
so what's best way of reading this?
you use combination of split(" ")
, splitat(1)
create tuple containing first element , rest:
scala> val s = "3 1 2 3" s: string = 3 1 2 3 scala> val (array(nb), rest) = s.split(" ").map(_.toint).splitat(1) nb: int = 3 rest: array[int] = array(1, 2, 3)
(obviously replace s
readline
in case).
Comments
Post a Comment