Zouhair Rouika
Zouhair Rouika

Reputation: 13

Unable to use .toChar() after readLine()

I am Unable to use the .toChar() in Kotlin after the readLine()!!

Like in This Case

//An Array Of Characters
    var CharAr= Array<Char>(5){' '}
    for(i in 0..4){
        println("Please Enter The character Number ${i+1}")
        CharAr[i]= readLine()!!.toChar()
    }

Or Even In This Case

//Normal readLine()
var CharacterNum1:Char
    println("Please Enter a Character")
    CharacterNum1= readLine()!!.toChar()

Thank You For Your Help :)

Upvotes: 0

Views: 324

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 82027

readLine() returns a String?. This type does not have a toChar method. What you can do is take the first char of that String like this:

charAr[i] = readLine()?.get(0) ?: throw IllegalArgumentException()

Upvotes: 3

Related Questions