Scala Unit type

前端 未结 5 1387
情深已故
情深已故 2020-12-13 18:32

I use opencsv to parse csv files, and my code is

while( (line = reader.readNext()) != null ) { .... }

I got a compiler warning saying:

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 19:06

    You are writing Scala code they way you would write it in Java. Try doing it in a more Scala-like way. To read a text file line by line and do something with each line, try this:

    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)
    }
    

提交回复
热议问题