lundi 26 août 2019

Is there a better way to know if the current read line is null in a read-while loop?

I would like to know if there is a functional approach in identifying no lines are left to be read in the while condition when reading from, e.g. standard input or a file.

I have tried the Option/Some/None pattern in several ways, but nothing seems to work better than the old Java null with != boolean comparison operation.

var a: String = null.asInstanceOf[String]
while( { a = scala.io.StdIn.readLine; a != null } ) {
namesMap.get( a ) match {
    case Some( value ) => println( s"$a=$value" )
    case None => println( "Not found" )
  }
}

Can you please give some advice you can have as experienced Scala programmers?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire