Search code examples
stringkotlininputintegerreadline

Which is better? Integer.valueOf(readLine()) or readLine()!!.toInt()


I am learning Kotlin using codeacademy. In the Madlibs project I was asked to read an 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())


Solution

  • toInt() is the better solution. It's in the Kotlin standard library, and Integer.parseInt is in the Java library. Always prefer Kotlin-only solutions over Java solutions, where available.