Kotlin Input Output

In this tutorial you will learn about the Kotlin Input Output and its application with practical example.

Kotlin Input Output

Kotlin Input Output statements are used to read input stream from any standard input device (ex. keyboard) to main memory and to send data stream to any standard output device (ex. monitor).

Koltin Output

In Kotlin, we have println() and print() functions available to send output to the standard output (screen).

Example:-

Output:-

The println() and print() outputs the given string as following.

Difference Between println() and print()

The print() function prints the given string, while println() function prints the given string similar like print() function. Then it moves the cursor to the beginning of the next line.

Kotlin Input

Kotlin readline() function is used to read a line of string from standard input device like keyboard.

Example:-

Output:-

When you run the program, it will prompt you for input then it will print the given string in screen.

Example2:-

Output:-

The readLine() function takes any input as a string and convert its to values of other data type (like Int) explicitly.

In Kotlin, if you want input of other data types as input the Scanner object. For that, you need to import Scanner class from Java standard library as following –

Getting Integer Input

Example:-

Output:-

Here, the nextInt() method takes integer input from the user then store it in an integer variable. To take Long, Float, double and Boolean input from the user, you can use nextLong(), nextFloat(), nextDouble() and nextBoolean() methods respectively.

In this tutorial we have learn about the Kotlin Input Output and its application with practical example. I hope you will like this tutorial.