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