Scala read first element of line separately -


i have following input on line:

  1. number of elements in array
  2. 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

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -