How to convert a String to an Int in Kotlin?
Hello everyone! I just started learning Kotlin and encountered a simple but confusing task. I have a string obtained from user input via console (readLine()), and I want to use it as an integer for arithmetic operations. For example, I'm writing a simple calculator that adds two numbers:\n\n```kotlin\nfun main() {\n print("Enter first number: ")\n val firstInput = readLine()\n print("Enter second number: ")\n val secondInput = readLine()\n \n val sum = firstInput + secondInput // Error here!\n println("Sum: $sum")\n}\n```\n\nOf course, this code doesn't work because firstInput and secondInput are strings, and the + operator just concatenates them instead of adding. I understand that I need to somehow convert the string to Int, but I don't know how to do it.
No answers yet.
Log in to leave an answer
Blogs
Asynchronous Programming in C#: A Complete Guide for .NET