0

I'm working on a program that read a file, and from this file, I need to get the numbers in a specific order.

All the numbers are on the same line, and separated by a tabulation. Like in this example :

d       s       a       m
2       1       0       1
3       2       1       1

In C++, that should look like that :

unsigned d, s, a;
infile >> d >> s >> a;

But I'm new in Scala, so I have no idea how to do.

I'm using scala.io.Source.

4

1 回答 1

2

如果您有一个str包含空格分隔数字的字符串(您可以使用 获得getLines()),您可以

val nums = str.
  split("\\s+").    // Splits at whitespace into an array of strings
  map(_.toInt)      // Converts all elements of array from String to Int

然后如果你想把前三个拉出来你可以

val Array(d,s,a) = nums.take(3)

或者

val (d,s,a) = (nums(0), nums(1), nums(2))

或其他各种事情。

于 2013-07-23T14:36:31.517 回答