Kotlin Program to Check if a String is Numeric

Kotlin Program to Check if a String is Numeric. To check if the string contains numbers only, in the try block, we use Double ‘s parseDouble() method to convert the string to a Double.

Kotlin Program to Check if a String is Numeric

Source Code

import java.lang.Double.parseDouble

fun main(args: Array<String>) {

    val string = "12345s15"
    var numeric = true

    try {
        val num = parseDouble(string)
    } catch (e: NumberFormatException) {
        numeric = false
    }

    if (numeric)
        println("$string is a number")
    else
        println("$string is not a number")
}

Output