Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 58 / 68

Reading from Standard Input

In this lesson you will learn how to read standard input with readLine in Kotlin, so your program can respond to what a user types. This is for anyone searching for "Kotlin readLine how to use".

readLine() reads one line of input from the keyboard or another source. Use it when a command-line tool needs the user to type something.

The sample code prompts without a newline using print("Enter your name: "), reads what was typed into name with readLine(), and uses it in a greeting.

A common stumbling block for beginners is that readLine() returns a nullable type. It does so out of null safety, so you need to understand the difference between using it directly and checking for null first.

In real-world development, using input without validating it can lead to unexpected errors, so pairing it with a validation step is safer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)