Read Data Input from Command Line in Kotlin

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