Kotlin Convert String to Int

In the tutorial, JavaSampleApproach will show you how to convert Kotlin String to Int.

Related posts:
Kotlin Convert String to Long


Working environment:
– Java 8
– Kotlin 1.1.61

Kotlin toInt() method

String.toInt(): Int

– use method signature: public inline fun String.toInt(): Int


package com.javasampleapproach.string2int

fun main(args : Array) {
	// use method:
	// -> public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)
	val number: Int = "123".toInt();
	println(number) // 123
	
	// if the string is not a valid representation of a number
	// -> throw NumberFormatException
	try{
		"12w".toInt();
	}catch(e: NumberFormatException){
		println(e.printStackTrace())
		// -> print on console:
		/*
		java.lang.NumberFormatException: For input string: "12w"
			at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
			at java.lang.Integer.parseInt(Integer.java:580)
			at java.lang.Integer.parseInt(Integer.java:615)
			at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:12)
		*/
	}
}

Strig.toInt() will throw a NumberFormatException if the string is not a valid representation of a number.
String.toInt() just uses Integer.parseInt of Java for converting
-> detail: public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)

String.toInt(radix: Int): Int

If we want to work with radix, we can use another method signature toInt(radix: Int)
-> detail: public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))


package com.javasampleapproach.string2int

fun main(args : Array) {
	// use method:
	// -> public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))
	val numberWithRadix2: Int = "101".toInt(2)
	println("numberWithRadix2 = $numberWithRadix2") // numberWithRadix2 = 5
	
	// if the string is not a valid representation of a number
	// -> throw NumberFormatException
	try{
		"123".toInt(2);
	}catch(e: NumberFormatException){
		println(e.printStackTrace())
		// -> print on console:
		/*
		java.lang.NumberFormatException: For input string: "123"
		at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
		at java.lang.Integer.parseInt(Integer.java:580)
		at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:12)
 		*/
	}
	
	// if radix is not a valid radix
	// -> throw IllegalArgumentException
	
	try{
		"101".toInt(-1)
	}catch(e: IllegalArgumentException){
		println(e.printStackTrace())
		// -> print on console:
		/*
		java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36
		at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155)
		at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:28)
		*/
	}
}

Exception:

  • String.toInt(radix: Int) will throw a NumberFormatException if the string is not a valid representation of a number.
  • String.toInt(radix: Int) will throw a IllegalArgumentException if the radix is not a valid radix.
  • String.toInt(radix: Int) just uses Integer.parseInt(this, checkRadix(radix)) of Java for converting,
    -> detail: public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))

    Kotlin toIntOrNull() method

    String.toIntOrNull(): Int?

    Method signature: public fun String.toIntOrNull(): Int?
    -> Parses the string to an Int number and returns a number or null in case the string is not a valid representation of a number.
    It does not throw any exception.

    
    package com.javasampleapproach.string2int
    
    fun main(args : Array) {
    	// use method:
    	// -> public fun String.toIntOrNull(): Int?
    	val number: Int? = "123".toIntOrNull()
    	println("number = $number") // number = 123
    	
    	val a: Int? = "12w".toIntOrNull()
    	println("a = $a") // a = null
    }
    

    String.toIntOrNull(radix: Int): Int?

    Method signature: public fun String.toIntOrNull(radix: Int): Int?
    -> Parses the string to an Int number and returns null in-case the string is not a valid representation of a number.
    And IllegalArgumentException will be thrown when radix is not a valid radix for string to number conversion.

    
    package com.javasampleapproach.string2int
    
    fun main(args : Array) {
    	// use method:
    	// -> public fun String.toIntOrNull(): Int?
    	val number: Int? = "101".toIntOrNull(2)
    	println("number = $number") // number = 5
    	
    	val a: Int? = "12w".toIntOrNull(2)
    	println("a = $a") // a = null
    	
    	val b: Int? = "192".toIntOrNull(8)
    	println("b = $b") // b = null
    	
    	try{
    		"101".toIntOrNull(-1)
    	}catch(e: IllegalArgumentException){
    		println(e.printStackTrace())
    		// -> print on console
    		/*
    		java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36
    		at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155)
    		at kotlin.text.StringsKt__StringNumberConversionsKt.toIntOrNull(StringNumberConversions.kt:182)
    		at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:16)
    		*/
    	}
    }
    


    By grokonez | December 14, 2017.

    Last updated on April 17, 2021.



    Related Posts


    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    *