How to Convert String to Int in Kotlin
-
Convert Kotlin
String
toInt
UsingtoInt()
Method -
Convert Kotlin
String
toInt
UsingtoIntOrNull()
Method -
Convert Kotlin
String
toInt
UsingparseInt()
Method
Suppose you are creating an application or a simple program. If a function in your program only accepts integer values, you must convert String
to Int
to prevent the NumberFormatException
or any other such exceptions.
But how do you convert Kotlin String
to Int
? There are multiple ways to do that. This article teaches a few ways to convert String
to Int
in Kotlin.
Convert Kotlin String
to Int
Using toInt()
Method
We can use the toInt()
function for converting a String
to an Int
. Here’s the standard syntax for doing that.
String.toInt()
This means we will call the toInt()
method on our String
instances for the conversion. In the below example, we will use this syntax for converting a String
"246"
to an Int
246
.
fun main() {
val strVal = "246"
val intVal = strVal.toInt()
println(intVal)
}
As you can see, the above program successfully converts the String
to Int
. But if there’s a non-convertible String
, the toInt()
function throws an InvalidFormatException
exception.
For the demonstration purpose, we will change the String
"246"
in the above code to String
"246b"
and try to convert it.
fun main() {
val strVal = "246b"
val intVal = strVal.toInt()
println(intVal)
}
This program throws java.lang.NumberFormatException
as it cannot convert the letter "b"
to an integer.
Hence, we must use the toInt()
function within a try-catch
block to handle the exception. With the try-catch
block, we can assign a default print value if the exception occurs.
Here’s how to incorporate the toInt()
function in the try-catch
block.
fun main(){
try {
val strVal = "246"
val intVal = strVal.toInt()
println(intVal)
} catch (e: NumberFormatException) {
println("This format is not convertible.")
}
try {
val strVal1 = "246b"
val intVal1 = strVal1.toInt()
println(intVal1)
} catch (e: NumberFormatException) {
println("This format is not convertible.")
}
}
The first toInt()
method converts the String
"246"
to an Int
in the above program. But when the second toInt()
function runs, it throws the NumberFormatException
, which is handled by the try-catch
block.
Convert Kotlin String
to Int
Using toIntOrNull()
Method
If we don’t want to add the try-catch
block, we can use the toIntOrNull()
function instead. As the name gives out, this method returns a null
value if the conversion is unsuccessful.
Hence, if you only want integer values and don’t want to worry about the non-convertible values, you can use the toIntOrNull
function.
fun main(){
val strVal = "246"
val intVal = strVal.toIntOrNull()
println (intVal)
}
If the conversion is successful, it will show the Int
value. But if the conversion is unsuccessful, it will convert the String
to null
.
fun main(){
val strVal = "246b"
val intVal = strVal.toIntOrNull()
println (intVal)
}
Convert Kotlin String
to Int
Using parseInt()
Method
Another way to convert Kotlin String
to Int
is using the parseInt()
function. It works similarly to the toInt()
method, with the key difference being that parseInt()
takes a parameter.
Another difference between the two is that toInt
belongs to the String
class, and parseInt()
is a function of the Kotlin Int
class.
Here’s how to use the parseInt()
method to convert Kotlin String
to Int
.
fun main(){
val strVal = "246"
val intVal = Integer.parseInt(strVal)
println(intVal)
}
Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.
LinkedIn