Hello eveyone
I am a beginner at kotlin
I am working on the following function
fun wordSelector(): Triple<String, List<String>, ArrayList<Int>> {
blanks.clear()
randomindex.clear()
val size = words.size
val randomise = (0 until size).random()
selectWord = words.elementAt(randomise)
val blankSize = selectWord.length-1
for (i in 0..blankSize){
checkArray[i] = selectWord[i].toString()
}
for (i in 0..blankSize) blanks.add("_")
for (i in 0..blankSize) {
val randomBlank = (0..blankSize-3).random()
if (randomindex.contains(randomBlank)) {
continue
}
else {
randomindex.add(randomBlank)
}
}
return Triple(selectWord, blanks, randomindex)
}
I am encountering the following error :
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
I’m getting this error at the following line of the code:
for (i in 0..blankSize){
checkArray[i] = selectWord[i].toString()
}
I am getting this out of bound error even when the size in for loop is -1 the actual size. (i.e blankSize = selectWord.length - 1)
Any solution?
Thank you
Suganesh