Read String Data Input in Kotlin
On this page (6sections)
Definition
In Kotlin, readLine() reads a line of input from the standard input stream or screen.
Syntax
var variable_name = readLine()
// Kotlin Defintion
fun readLine(): String? (source)
Read String in Kotlin Example Program
// Read String in Kotlin example program
// Data Input Kotlin Programs, Basic Kotlin Programs
fun main(args: Array < String > ) {
println("Pleas enter the Name")
var name = readLine()!!
println("Name is : $name")
}
Sample Output
Pleas enter the Name
GOODMAN
Name is :GOODMAN
How It Works
Here is a step-by-step walkthrough of how the Kotlin program for Read String Data Input in Kotlin runs, line by line. Following the flow of the code makes it clear how each statement contributes to the final result:
fun main(args: Array < String > ) {— defines a function used by the program.println("Pleas enter the Name")— writes a line of output shown in the sample output.var name = readLine()!!— reads a value entered by the user.println("Name is : $name")— writes a line of output shown in the sample output.
Key points to remember:
- Read the code top to bottom and match each line to the output it produces.
- Compile and run the example yourself rather than only reading it.
- Modify one statement at a time so you can see exactly what each change does.
After running it, compare your console output with the Sample Output above to confirm the program behaves as expected. Try changing the values and re-running the program to see how the result changes — experimenting with small edits is the fastest way to understand the logic and to remember the Kotlin syntax used in this example.
Frequently Asked Questions
What is Read String Data Input in Kotlin?
In Kotlin, readLine() reads a line of input from the standard input stream or screen.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Read String Data Input?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.