I am beginner at kotlin and I am trying to find if the input letter is in the range of the letters
fun main(){
var letters = 'a'..'f' step(2)
var yourLetter = readLine()!!.toCharArray()
if(yourLetter in letters){
print("that's here")
}else{
print("that's not here")
}
}
the code return error in
Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
how can I solve this problem ?
CodePudding user response:
both letters
and yourLetter
are collections of chars. if your input is "abc" for example then yourLetter
will become ['a','b','c']
. If you only want to look at the first character that was provided you could do
fun main(){
var letters = 'a'..'f' step(2)
var yourLetter = readLine()!![0] //note: you don't need to convert to CharArray
if(yourLetter in letters){
print("that's here")
}else{
print("that's not here")
}
}
if you want to check if any of the input characters are in it you could do
fun main(){
var letters = 'a'..'f' step(2)
var yourLetter = readLine()!!
if(yourLetter.any {it in letters}){
print("that's here")
}else{
print("that's not here")
}
}