import kotlin.reflect
fun main(args: Array<String>) {
val cons = listOf("B","C","D", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z",)
var str = "JBIKCA"
for (item in str.indices) {
val j = (str[item])
if (j in cons){
println(j)
}
}
Apologies for the basic question but this is my first time with the language.
Correct Output should be:
J
B
K
C
CodePudding user response:
The problem is that j
is a Char
where cons
contains String
elements.
Simply convert the characters to string before checking. You can also more simply iterate str
using for..in
for (j in str) {
if (j.toString() in cons) {
println(j)
}
}