kotlincompiler-errorssumnumbers

Summing numbers in Kotlin


fun main() {

    var numbers =("1,2,3,4,5,6")

    val result = numbers . sum () 

    println ("$result ") 
}

I wanted to calculate these numbers with the sum function, but it gives me an error that sum is unused. What should I do to solve it?


Solution

  • Your numbers variable is not an array of ints, it's a string - note the quotes (") you have there. As such, it doesn't have a sum method, and hence the error.

    You can use arrayOf to define an array:

    fun main() {
        var numbers = arrayOf(1,2,3,4,5,6)
        val result = numbers.sum()
        println ("$result")
    }