Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala equivalent of java.util.Scanner

Tags:

parsing

scala

I am very familiar with using java.util.Scanner with next(), hasNext(), nextInt(), nextLine(), and the like to parse input.

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.

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 

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 obviously does the trick. But is there something else I should use (for example, something that returns Scala rather than Java types)?

like image 371
Paul Draper Avatar asked Dec 26 '22 19:12

Paul Draper


1 Answers

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.

So as for "for example, something that returns Scala rather than Java types", Scanner will return scala types when used in scala.

like image 58
Harshal Pandya Avatar answered Jan 11 '23 23:01

Harshal Pandya