Read Data Input from Command Line in Kotlin
On this page (7sections)
Definition
- While compiling and running a Kotlin Program through the command line, the user input can be passed along with the command to run the program.
- The arguments passed are separated by spaces( ).
Compiling the Kotlin Program
kotlinc ProgramName.kt
Running the Kotlin Program
kotlin ProgramNameKt Hey Kotlin Programmer
Here “Hey Kotlin Programmer” is the argument passed. Here, “Hey”, “Kotlin” and “Programmer” are separate arguments.
Read Input from Command Line in Kotlin Example Program
// Read Input from Command Line in Kotlin Example Program
// Data Input Kotlin Programs, Basic Kotlin Programs
fun main(args: Array<String>) {
println("Printing all command line arguments: ")
//Printing all arguments passed through command line
for (item in args){
println("Input argument : $item")
}
}
Sample Output
Printing all command line arguments :
Input argument : Hey
Input argument : Kotlin
Input argument : Programmer
How It Works
This Kotlin program demonstrates Read Data Input from Command Line. It first prepares the data it needs, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Iterate over the data using a loop to apply the logic.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Frequently Asked Questions
What is Read Data Input from Command Line in Kotlin?
While compiling and running a Kotlin Program through the command line, the user input can be passed along with the command to run the program.
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 Data Input from Command Line?
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.