In the Madlibs project, I was asked to read a number input using readLine()
from the user and then convert it to an integer. I want to know which of the two solutions is better moving forward.
This was my solution: distance = readLine()!!.toInt()
This was the given solution: distance = Integer.valueOf(readLine())
1 Like
I think when dealing with user input there should be some error handling along the way. In this sense, the assertion !!
doesn’t seem as good. But Integer.valueOf(readLine())
can potentially break the program if it’s not wrapped in some try/catch block, or you can guarantee that the input keyboard will only have integers strictly (like a numpad without a dot for decimals).
1 Like