andy lacron
andy lacron

Reputation: 71

inferred type is String but Int was expected

a new Kotlin learner Trying understand basics and been trying to add integers from a console input variable to a mutablelist but i get the error (Type mismatch: inferred type is String but Int was expected). I seem not to tell where the issue is.

fun main() {

    val amount = mutableListOf<Int>()           //Mutable list to store the Ints

    println("Add an amount to the list: ")

    val money = readLine()!!.toInt()            //Requesting for an IntInput

    amount.add(6000)                            //Adding Ints to the list

    amount.add(8000)                            //Adding Ints to the list

    amount.add("$money")                        **LINE WITH THE ERROR IS THIS ONE WHEN PASSING THE MONEY VARIBALE THAT HOLDING THE INT INPUT** 

    println("$amount")                          //Printing the list items
}

Upvotes: 0

Views: 770

Answers (1)

broot
broot

Reputation: 28292

"$money" creates a string that contains the value of money. You need to use money directly:

amount.add(money)

Upvotes: 1

Related Questions