开发者

readInt for scala.io.Source

开发者 https://www.devze.com 2023-03-06 23:25 出处:网络
In Console.scala we\'re having nice methods such as readInt, readChar, etc. W开发者_如何学Pythonhy don\'t we have them for io.Source? And if you argue we should use Java\'s IO capabilities, then why

In Console.scala we're having nice methods such as readInt, readChar, etc.

W开发者_如何学Pythonhy don't we have them for io.Source? And if you argue we should use Java's IO capabilities, then why having the Console.read* methods?

Any other scala-ish method for minimal file parser?


How about:

val sc = new java.util.Scanner (System.in)


I'm not sure it's worthwhile adding this to the standard library. Console.readInt reads a whole line and then converts it to an int. Presumably the use case is when the user is prompted to enter an integer at the console. It's not coded in a defensive way either, try entering a space after the number like '6 '

When using readChar all the characters in the line are discarded except for the first one.

When using io.Source the use case is more about iterating by char or by line. Processing the input with the same semantic as the Console.read* method should be rare.

I just saw user unknown's answer and I think it's good.

0

精彩评论

暂无评论...
验证码 换一张
取 消