Create a program that ask a user to type positive integers. Each positive integer is saved to a file called numbers.txt. When a user types -1, the program reads the file, and add numbers to a list. Finally, the program prints the count and the average of typed numbers from a list. If the typed integer is not positive, the program prints "The number must be positive" message.
So far, my program is as follows:
import java.util.*
import java.io.File
fun main(args: Array<String>) {
val list = mutableListOf<Int>()
var counter = 0
while(true){
val reader = Scanner(System.`in`)
var file = File("numbers.txt")
println("Type a positive integer (-1 to exit):")
var input_num = reader.nextInt()
if (input_num >= 0) {
list.add(input_num)
counter++
}
else if (input_num < -1){
println("The number must be positive")
}
else if (input_num == -1){
break
}
}
println("You typed $counter numbers.")
println("The average is ${list.average()}.")
}
The output is working as expected 1) counter displays the desirable number and 2) the average of the list is also displayed correctly.
Although, my issue is that no numbers were added to the actual .txt file. I am sorry for any mistake made on by behalf this is my first post and I am completely new to Kotlin language. Any feedback is very appreciated.