我使用opencsv来解析csv文件,我的代码是
while( (line = reader.readNext()) != null ) { .... }
Run Code Online (Sandbox Code Playgroud)
我收到编译器警告说:
comparing values of types Unit and Null using `!=' will always yield true
[warn] while( (aLine = reader.readNext()) != null ) {
Run Code Online (Sandbox Code Playgroud)
我应该怎么做while循环?
mkn*_*ssl 71
赋值表达式Unit在Scala中具有类型.这就是编译器警告的原因.
Scala中有一个很好的习惯用法可以避免while循环:
val iterator = Iterator.continually(reader.readNext()).takeWhile(_ != null)
Run Code Online (Sandbox Code Playgroud)
这为您提供了任何reader.readNext返回的迭代器.
该continually方法返回一个"无限"迭代器并takeWhile获取其前缀,但不包括第一个null.
(斯卡拉2.8)
Vas*_*iuk 20
在你的情况下(line = reader.readNext())是一个返回类型Unit的功能文字.您可以按如下方式重写代码:
while( {line = reader.readNext(); line!= null} ) { .... }
Run Code Online (Sandbox Code Playgroud)
oxb*_*kes 17
你可以用a Stream来得到你想要的东西:
Stream.continually(reader.readLine()).takeWhile( _ ne null) foreach { line =>
//do Stuff
}
Run Code Online (Sandbox Code Playgroud)
这还有其他很酷的东西的额外优势:
Stream.continually(reader.readLine()).takeWhile( _ ne null) match {
case head #:: tail => //perhaps you need to do stuff with the first element?
case _ => //empty
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 感谢mkneissl指出我应该包括这个警告:
scala> Stream.continually(1).take(100000000).foreach(x=>())
scala> val s = Stream.continually(1).take(100000000)
s: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> s.foreach(x=>()) java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
Jes*_*per 10
您正在编写Scala代码,就像用Java编写代码一样.尝试以类似Scala的方式执行此操作.要逐行读取文本文件并对每行执行某些操作,请尝试以下操作:
import java.io.File
import scala.io.Source
Source.fromFile(new File("myfile.txt")).getLines.foreach { line =>
// Do something with the line, for example print it
println(line)
}
Run Code Online (Sandbox Code Playgroud)