我尝试使用scala中的 java.util.Scanner :
val scanner = new java.util.Scanner(System.in)
val line = scanner.readLine()
println(line)
通过粘贴scala REPL或运行scala file.scala
,我得到了这个:
:2: error: value readLine is not a member of java.util.Scanner
val line = scanner.readLine()
我错过了什么?
答案 0 :(得分:4)
java.util.Scanner
没有readLine
尝试nextLine
。
scala> val scanner = new java.util.Scanner(System.in)
scanner: java.util.Scanner = java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
scala> val line = scanner.nextLine()
line: String = foo
答案 1 :(得分:2)
最简单的方法似乎是使用JavaConverters
将Scanner
用作Iterator
:
import scala.collection.JavaConverters._
...
val input = new Scanner(System.in).asScala
input.head // first element
val asList = input.toList // scala list