Given a string input with numbers separated by whitespaces, Generate a code in Kotlin that uses readln to read the data and converts each number to an integer. Then, determine the sum of these integers and output it. For example, if your string input is '2 4 6', convert those to integers and print the sum; in this case 12.
Reading data with readln
Summing up space-separated integers from a string
Report a typo
Sample Input 1:
1 3 5Sample Output 1:
9Sample Input 2:
2 4 6Sample Output 2:
12Write a program in Kotlin
// Importing necessary libraries
import java.util.Scanner
fun main(args: Array<String>) {
// Initialisation of a Scanner
val scanner = Scanner(System.`in`)
// TODO: Get input string line
// TODO: Use split to create an array
// TODO: Iterate over array to convert from String to Int and accumulate the sum
// TODO: Print the sum.
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.