Reputation: 3
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.
Upvotes: 0
Views: 282
Reputation: 167901
If you have a string str
containing whitespace-separated numbers (which you can get with getLines()
), you can
val nums = str.
split("\\s+"). // Splits at whitespace into an array of strings
map(_.toInt) // Converts all elements of array from String to Int
and then if you want to pull the first three out you can
val Array(d,s,a) = nums.take(3)
or
val (d,s,a) = (nums(0), nums(1), nums(2))
or various other things.
Upvotes: 2