Convert String to Integer in Kotlin
To convert a string to integer in Kotlin, use String.toInt() or Integer.parseInt() method. If the string can be converted to a valid integer, either of the methods returns int value.
You may need to convert a string to integer in scenarios like: extracting numbers from string messages and perform some arithmetic operations on them; you receive a value as string from outside your program, but you are treating it as an integer in your application; etc.
In this tutorial, we shall learn different ways of how to convert a string to integer and different scenarios where we may need to use this conversion.
Syntax
The syntax of String.toInt() is given below.
String.toInt()
String.toInt() returns int value if conversion is successful. Else, it throws java.lang.NumberFormatException.
The syntax of Integer.parseInt() is given below.
Integer.parseInt(String)
Integer.parseInt() function takes the string as argument and returns int value if conversion is successful. Else, it throws java.lang.NumberFormatException same as that of String.toInt().
Examples
1. Convert String to Integer using String.toInt()
In this example, we shall first initialize a string. Secondly we call toInt() on the string and store the returned int value. You can use this int value as per your requirement, but in this example, we are just printing it.
example.kt
/**
* Kotlin - Convert Sting to Integer
*/
fun main(args: Array<String>) {
//a string
val str = "241"
//convert string to integer
val num = str.toInt()
print(num+10)
}
Run this Kotlin program. Just to prove that it is an int, we are adding a value of 10 to it and printing the result.
Output
251
2. Convert String to Integer using Integer.parseInt()
In this example, we shall first initialize a string. Secondly we call Integer.parseInt() with the string as arguemnt the string and store the returned int value.
example.kt
/**
* Kotlin - Convert Sting to Integer
*/
fun main(args: Array<String>) {
//a string
val str = "241"
//convert string to integer
val num = Integer.parseInt(str)
print(num+10)
}
Run this Kotlin program. Like in the previous example, we are adding a value of 10 to the integer and printing the result.
Output
251
3. Convert String to Int – Negative Scenario
In this example, we shall try to convert a string to integer, where the string, as a whole, does not represent a valid integer. Let us see what happens. Certainly, str.toInt() throws an exception.
example.kt
/**
* Kotlin - Convert Sting to Integer
*/
fun main(args: Array<String>) {
//a string
val str = "241a"
//convert string to integer
val num = str.toInt()
print(num+10)
}
Run this Kotlin program, and you will get the following output.
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "241a"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at KotlinStringToIntegerKt.main(KotlinStringToInteger.kt:8)
Yeah, as we have already mentioned, an Exception occurred. Specifically it is java.lang.NumberFormatException.
Conclusion
In this Kotlin Tutorial, we learned how to convert a string to integer using different methods.