【发布时间】:2018-07-14 20:24:53
【问题描述】:
我在这里很新,所以如果我把它放在正确的部分,我就不是。我对 Scala 也很陌生。
无论如何,所以我正在尝试从文本文件中读取数字(我猜它们在这里是字符串)并将它们分成对,按照它们被读取的顺序,但我无法处理:
- 新行
- 将一组数字拆分成对
这是我的代码:
def main(args: Array[String]): Unit = {
val file = Source.fromFile("/Users/donatkapesa/Desktop/poly.txt")
val fileLines = file.getLines()
while(fileLines.hasNext && !fileLines.isEmpty) {
val array = fileLines.next.split(" ")
//this doesn't take care of new lines. I have tried .split(" +") and .split("\\s+")
// change the array elements to integers
val intArray = array.map(array => array.toInt)
// split array elements into tuples. But it doesn't work
val coordinates = intArray.map(case Array(x,y) => (x,y))
}
【问题讨论】: