I am working on a console application in Kotlin where I accept multiple arguments in main()
function
fun main(args: Array) {
/
As suggested above, use toIntOrNull()
.
Parses the string as an [Int] number and returns the result or
null
if the string is not a valid representation of a number.
val a: Int? = "11".toIntOrNull() // 11
val b: Int? = "-11".toIntOrNull() // -11
val c: Int? = "11.7".toIntOrNull() // null
val d: Int? = "11.0".toIntOrNull() // null
val e: Int? = "abc".toIntOrNull() // null
val f: Int? = null?.toIntOrNull() // null