toint

Scala - How to convert a String to an Int (Integer)

Scala FAQ: How do I convert a String to Int in Scala?

If you need to convert a String to Int in Scala, just use the toInt method which is available on String objects, like this:

scala> val i = "1".toInt
i: Int = 1

As you can see, I just cast a String (the string "1") to an Int object using the toInt method on the String.

However, beware that this can fail just like it does in Java, with a NumberFormatException, like this:

Syndicate content