In Scala, there is a huge library to support different operations on a string. One such operation is to convert string to int in Scala.
A string can be converted to integer in Scala using the toInt method.
Syntax:
string.toInt
This will return the integer conversion of the string. If the string does not contain an integer it will throw an exception with will be NumberFormatException.
So, the statement: val i = "Hello".toInt will throw an exception instead of an integer.
So, we need to handle this exception and we will do this using the try-catch block and print "The string in not integer" when the string to be converted does not contain an integer value.
Program to convert string to integer in Scala
Output
need an explanation for this answer? contact us directly to get an explanation for this answer