mercredi 4 décembre 2019

kotlin Get random string from array

New to kotlin and quite a few questions and answers, mostly in Java. After following the documentation and verifying against numerous SO questions/answers Android Studio is complaining about the following in a function. The function is

fun getRandomQuote() {
    val randomValue = Random.nextInt(quotes.size)
    //return quotes[randomValue]
    return quotes.get(randomValue)
}

quotes is an arrayOf(strings)

val quotes = arrayOf("Alert today – Alive tomorrow.",...)

It says for

quotes.get(randomValue)

That it should be a Unit but found String. randomValue should be an integer as defined by the docs using nextInt unless I misinterpreted the docs. I don't see the issue. I'm just trying to randomly return a string from the array. I thought maybe another false positive from studio so I cleaned the project but it stops here on building. Anyone see what it is I'm doing wrong.




Aucun commentaire:

Enregistrer un commentaire