String into int

Hey guys. I am trying to create a code that will take that string of numbers separated with space and change them into integers.

val string1 = ("-3 3 4 5 6")
println(string1)
var ints = string1.split(",").map {it.toInt()}

Is it any close to a correct solution?


I don’t really understand that message

You need to split the string on spaces, not on a comma. The error message is that β€œ-3 3 4 5 6” is not a valid number.
Try string1.split(" ") instad.

2 Likes

You are splitting by β€˜,’ which does not exist in your source string.
Prefer .replace(β€œ β€œ, β€œβ€) instead of splitting and then mapping

string1.replace(" β€œ, β€œβ€).toInt()

Also you might want to surround it by try catch just to catch the exception

1 Like