Scala等效于java.util.Scanner [英] Scala equivalent of java.util.Scanner
问题描述
我对将java.util.Scanner
与next()
,hasNext()
,nextInt()
,nextLine()
等用于解析输入非常熟悉.
I am very familiar with using java.util.Scanner
with next()
, hasNext()
, nextInt()
, nextLine()
, and the like to parse input.
我应该在Scala中使用其他东西吗?
Is there something else I should use in Scala?
此数据不是根据语法来构造的;比这更特别.
This data isn't structured according to a grammar; it's more ad-hoc than that.
例如,假设我有一个库存.输入的每一行均以名称开头,然后具有这些项目的数量,然后具有这些项目的ID
For example, lets say I had a inventory. Each line of input starts with the name, then has the quantity of those items, then has the ids for those items
Firetruck 2 A450M A451M
Machine 1 QZLT
Keyboard 0
我看到Console
具有诸如readInt()
之类的方法,但是它读取了整行输入;似乎不存在nextInt()
的等效项.
I see that Console
has methods such as readInt()
, but that reads an entire line of input; the equivalent of nextInt()
doesn't seem to exist.
java.util.Scanner
显然可以解决问题.但是我还有其他需要使用的东西(例如,返回Scala而不是Java类型的东西)吗?
java.util.Scanner
obviously does the trick. But is there something else I should use (for example, something that returns Scala rather than Java types)?
推荐答案
不存在等效的Scala实现.但是我看不出有什么原因,因为java.util.Scanner可以很好地工作,并且所有java原语类型都将隐式转换为Scala类型.
No there is no equivalent Scala implementation. But I don't see a reason for one as java.util.Scanner would work perfectly fine and all java primitives types would be converted to Scala types implicitly.
例如,对于返回Scala而不是Java类型的东西",Scanner在scala中使用时将返回scala类型.
So as for "for example, something that returns Scala rather than Java types", Scanner will return scala types when used in scala.
这篇关于Scala等效于java.util.Scanner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!