i get an error when i start filling my array and here the code

fun main(args :Array<String>){
println(" give the size")
var nbr=readLine()!!.toInt()
    class time(var hour:Int,var minute:Int,var seconde:Int)
class athlete(var name:String,var nombre:Int, var result:time)
var tab=arrayOfNulls<athlete>(50)
for(i in 0 until nbr)
    println("give the name of  ${i+1} athlete")
tab[i].name=readLine()!!
println("give the nombre of the athelte")
tab[i].nombre=readLine()!!.toInt() 
println("give the hours")
 tab[i].time.hour=readLine()!!.toInt()
println("give the minute")
tab[i].time.minute=readLine()!!.toInt()   
println("give the seconds")
tab[i].time.seconde=readLine()!!.toInt()

the errors are in the readLine when i start filling the arrays

2 Answers

0
Boken On

You forget to add brakcets in for loop:

fun main() {
    println(" give the size")
    val nbr = readLine()?.toInt() ?: 0

    val tab = arrayOfNulls<Athlete>(nbr)

    for (i in 0 until nbr) {
        val athlete = Athlete()
        val time = Time()

        println("give the name of  ${i + 1} athlete")
        athlete.name = readLine()

        println("give the nombre of the athlete")
        athlete.nombre = readLine()?.toInt()

        println("give the hours")
        time.hour = readLine()?.toInt()

        println("give the minute")
        time.minute = readLine()?.toInt()

        println("give the seconds")
        time.second = readLine()?.toInt()

        athlete.result = time

        tab[i] = athlete
    }
}

class Time {
    var hour: Int? = null
    var minute: Int? = null
    var second: Int? = null
}

class Athlete {
    var name: String? = null
    var nombre: Int? = null
    var result: Time? = null
}

0
ARGeo On

You need to use class properties instead of parameters.

Here's how your code should look like:

class Time {
    var hour: Int = 0
    var minute: Int = 0
    var seconde: Int = 0
}  
class Athlete {
    var name: String = " "
    var nombre: Int = 0
    var result = Time()
}

fun main(args: Array<String>) {

    println("Give the size")
    var nbr: Int = readLine()!!.toInt()
    var tab1 = arrayOfNulls<Athlete>(50)
    var tab2 = arrayOfNulls<Time>(50)

    for (i in 0 until nbr) {

        println("Give the name of ${i+1} athlete")
        tab1[i]?.name = readLine()!!

        println("Give the nombre of the athelte")
        tab1[i]?.nombre = readLine()!!.toInt() 

        println("Give the hours")
        tab2[i]?.hour = readLine()!!.toInt()

        println("Give the minute")
        tab2[i]?.minute = readLine()!!.toInt() 

        println("Give the seconds")
        tab2[i]?.seconde = readLine()!!.toInt()
    }
}