1. Overview

In Kotlin, there are various scenarios where we might need to convert a String array to an Int array. In this article, we’ll explore different approaches to the problem. Additionally, we’ll show how to handle possible exceptions during the conversion.

2. Using the toInt() Function

In its simplest scenario, a straightforward approach is to iterate through each String array element and use the toInt() function. It converts each element to an Int instance. Let’s see an example:

@Test
fun `when using toInt then it converts array`() {
    val stringArray = arrayOf("1", "2", "3", "4", "5")
    val intArray = stringArray.map { it.toInt() }.toIntArray()
    assertThat(intArray.all { it is Int }).isTrue()
    assertThat(intArray).isEqualTo(arrayOf(1, 2, 3, 4, 5))
}

In this example, the map function transforms each element of stringArray to an Int. After that, we convert the resulting list to an array using the toIntArray() function.

3. Handling Possible Number Format Exceptions

While the above method is concise, it doesn’t handle cases where the conversion might fail due to invalid formats in the String array. Let’s demonstrate this with an example:

@Test
fun `when using toInt then exception is thrown`() {
    val stringArray = arrayOf("1", "2", "3", "four", "5")
    assertThrows<NumberFormatException> { stringArray.map { it.toInt() }}
}

The value “four” can’t be converted to an Int and throws a NumberFormatException. To address this, we’ll implement a safer approach to error handling:

@Test
fun `when using toInt then exception is handled`() {
    val stringArray = arrayOf("1", "2", "3", "four", "5")
    val intList = mutableListOf<Int>()
    for (element in stringArray) {
        try {
            val intValue = element.toInt()
            intList.add(intValue)
        } catch (e: NumberFormatException) {
            // Handle the case when provided value is not a number
        }
    }
    val intArray = intList.toIntArray()
    assertThat(intArray).isEqualTo(arrayOf(1, 2, 3, 5))
}

In this example, a try-catch block handles potential NumberFormatException that might occur during the conversion. Additionally, this allows us to identify and handle elements that cannot be converted to integers.

4. Utilizing the toIntOrNull() Function

Finally, Kotlin provides the toIntOrNull() function. It’s the most convenient way to transform a String array into an Int array. It handles possible exceptions and returns Null instead of throwing exceptions. Let’s see it in action:

@Test
fun `when using toIntOrNull then it converts array`() {
    val stringArray = arrayOf("1", "2", "3", "four", "5")
    val intArray = stringArray.mapNotNull { it.toIntOrNull() }.toIntArray()
    assertThat(intArray.all { it is Int }).isTrue()
    assertThat(intArray).isEqualTo(arrayOf(1, 2, 3, 5))
}

Here, the function toIntOrNull() converts each element to an Int. Additionally, it returns Null in case conversion fails. After that, we use the mapNotNull() method to filter out elements that cannot be converted successfully. Thanks to that, we receive only integers. Moreover, we can transform it into an Int array with the toIntArray() method.

5. Conclusion

In this article, we demonstrated how to convert a String array to an Int array in Kotlin. Additionally, we showed how to handle potential exceptions. While the toInt() function provides a concise solution, handling exceptions or using toIntOrNull() ensures a more robust conversion process.

As always, the source code of the examples is available over on GitHub.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments