kotlin question Array ...... | Sololearn: Learn to code for FREE!


kotlin question Array ......

Day of the Week The given program declares an array of weekday names. You need to take a number as input and output the name of the day at that index. In case the input is out of the range, output "Invalid day". Sample Input: 2 Sample Output: Tuesday here is my Answer fun main(args: Array<String>) { val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") val day = readLine()!!.toInt() if (day >= 0) { println(names[day]) } } it passed all the test except the Fifth Test...... please help me to fix this ........

5/1/2021 9:34:01 AM

Abhishek Biswas

2 Answers

New Answer


Hi! you don't have a valid input check for a given range, and what should your program output if the data is out of bounds? read the task carefully again


#try this: fun main(args: Array<String>) { val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") var day = readLine()!!.toInt() if(day <= 6 && day >= 0) { println(names[day]) } else { println("Invalid day") } }