Hello good night everyone, every time I fill an array in Kotlin manually I get this type of error, will you know how to solve it?
import java.lang.System.`in`
import java.util.Scanner
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
val Teclado = Scanner(System.`in`)
var Valor=Teclado.nextInt()
print("Igrese el Tamaño del arreglo: ")
val Tamanio = scanner.nextInt()
var Silvia=IntArray(Tamanio)
for (Iteracion in 1..Tamanio){
print("Posicion[ $Iteracion ]: ")
Silvia[Iteracion]= Teclado.nextInt()
}
}
Thanks in advance
That error means that an element does not exist at the position you are trying to access.
Array positions start from 0. Therefore, in an array of 10 elements, position 10 does not exist.
You can subtract one from the variable when you try to access it, like this:
Or use the
indices
array property.