dictionary - Scala map split input to int and string? -
normally if reading line of input stdin looks "100 200" can store them both ints line:
val array(a, b) = readline.split(" ").map(_.toint);
but if a
integer , b
string?
you cannot store values of different types homogeneous container such array
. if know cardinality, store them in separate variables.
val input = "100 foo" val array(a, b) = input.split(" ") val p1 = scala.util.try(a.toint) val p2 = b
i used scala.util.try
because toint
may fail , throw exception
Comments
Post a Comment