Input and Output in Kotlin Programming language

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners



Do you know answer to below questions –
How to take input and output in kotlin programming language?
What is print() and println() method?
Difference between print() and println() method?
What is difference between readLine() and Scanner class?

If NO, get answer to such questions in this post. We will discuss about different ways to take input and output in kotlin programming language i.e. you will see how to show the input taken by the user to the screen. At the end, There are also some examples given to show how to take input and output in kotlin.

How To Print Output in Kotlin?

You can print output in kotlin in following ways –
a. Using print() method
b. Using println() method
c. Using Scanner class

Let’s go through each way one by one.

a. Print Output Using print() Method in Kotlin

You can send output to the screen using print() method. For example,

fun main(args : Array<String>) {
    print("Hello World")
}

When you run the program, you will get output as below,
Output: Hello World

1. Guess the output of the following program –

fun main(args : Array<String>) {
    print("Hello Kotlin")
}

How does print method work in kotlin?

print() method uses System.out.print() method to print any message passed to it. This method internally calls System.out.print() method to perform the task. You can pass value of any data type to print i.e. you can pass String, Char, Long, Int, Float, Double etc. data type value to print to the console.

Did you know?
You can check any method to know how does it work internally. Follow the steps to know.
If you are using IntelliJ or Android Studio
  a. Put your mouse on the variable.
  b. Press Ctrl + b or Press Cmd + b(for mac).
  c. You will be redirected to the method which is being called internally.

You can use this trick to know about methods being used for input and output in kotlin.

Apply above technic for print() method. You will be redirected to Console.kt file where you will get different types of definitions for this method.

Definitions of print method in Console.kt file
/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Any?) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Int) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Long) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Byte) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Short) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Char) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Boolean) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Float) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Double) {
    System.out.print(message)
}

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: CharArray) {
    System.out.print(message)
}

This means there is a method for each data type to print output to console. When you call print(23.0F), following methods will be called

/** Prints the given message to the standard output stream. */
@kotlin.internal.InlineOnly
public inline fun print(message: Float) {
    System.out.print(message)
}

Similarly, depending upon the parameter passed, method will be called.

b. Print Output Using println() Method in Kotlin

You can also print output to console using println method. Like print() method, println() method calls System.out.println() method to send output to the console. You can pass value of any data type (Float, Double, Char, String, Int, Long etc. ) as a parameter to the function. For example,

fun main(args : Array<String>) {
    println("Hello Kotlin")
}

Output: Hello Kotlin

What is difference between println() and print() method?

print(): This method prints value passed as parameter. But, Cursor remains at end in the same line.
println(): This method prints value passed as parameter and moves the cursor to beginning of next line.

For example,
1. What is output?

fun main(args : Array<String>) {
    print("Hello ")
    println("Kotlin")
}

Output: Hello Kotlin
Explanation: print(“Hello”) will print Hello and cursor will remain in same line. Next, println(“Kotlin”) will print Kotlin and moves the cursor to next line.

2. What is output?

fun main(args : Array<String>) {
    println("Hello ")
    print("Kotlin")
}

Output:
Hello
Kotlin

Explanation: println(“Hello”) will print Hello and moves the cursor to beginning of next line. Next, print(“Kotlin”) will print Kotlin and cursor will remain in same line.

– Exercise

1. What is Output?

fun main(args : Array<String>) {
        println(23.05f)

        println(23.05F + 23)
        print(23.05 + 23)

        println("Rohan is a ")
        println("Good boy")

        print("Rohan is a ")
        println("Good boy")
}



How to Take Input From User in Kotlin?

In this section, you will learn how to take input from user in kotlin programming language. You can take input from user in following ways in kotlin –

a. Using readLine() method
b. Using Scanner class

We will go through these one by one

a. Take input from User using readLine() method in Kotlin

To read a line of input from the standard input stream, you can use readLine() function. For example,

fun main(args : Array<String>) {
    var input = readLine()!!
    print("You entered = $input")
}

Output: Run the program and enter “Hello Kotlin”, you will get below output

You entered = Hello Kotlin

– Exercise,

Please try to answer it first. The, see the solution.
1. What is output of the program?

fun main(args : Array<String>) {
	val (a, b) = readLine()!!.split(' ')

	println(a.toInt() + b.toInt())

}

2. What is output of the program?

fun main(args : Array<String>) {
	val (a, b) = readLine()!!.split(' ').map(String::toInt)

	println(a + b)
}

b. Take Input from User using Scanner class in Kotlin

If you want to take input from user other than String data type, you need to use Scanner class. You have to create object of this class and use it to get input from user. For example,

val reader = Scanner(System.'in')

Here, reader is an object of Scanner class. Now, we will use this object to get input from user.

Note: Do not forget to import Scanner class into the file where you are creating object.

import java.util.Scanner

– Example,

Take integer input from user using Scanner
import java.util.Scanner

fun main(args: Array<String>) {

    // Create an object of Scanner class that is used to take input form standard input.
    val reader = Scanner(System.`in`)

    print("Enter a number: ")

    //nextInt() method is used to take next integer from keyboard and store it in enteredValue variable.
    var enteredValue:Int = reader.nextInt()

    println("You entered: $enteredValue")
}

When you run the program, You will get below output

Enter a number: 25
You entered: 25

Similarly, you can use different methods for input and output in kotlin. For example, Float input using reader.nextFloat(), Double input using reader.nextDouble(), boolean input using reader.nextBoolean() method etc.




Examples to show input and output in Kotlin

a. Print a Float value entered by an user in kotlin using Scanner.

Solution –

import java.util.Scanner

fun main(args: Array<String>) {

    // Create an object of Scanner class that is used to take input form standard input.
    val reader = Scanner(System.`in`)

    print("Enter a Float value: ")

    //nextFloat() method is used to take next float from keyboard and store it in enteredValue variable.
    var enteredValue: Float = reader.nextFloat()

    println("You entered: $enteredValue")
}

Output:
Enter a Float value: 25.09
You entered: 25.09

b. Print a Float value entered by an user in kotlin without using Scanner.

Solution –

fun main(args: Array<String>) {

    print("Enter a Float value: ")

    Here, readLine() method takes input from standard input and !! ensures that value is not null
    val stringInput = readLine()!!

    // .toFloat() function converts the string input to float
    var floatValue: Float = stringInput.toFloat()

    // prints the converted float value.
    println("You entered: $floatValue")
}

Output:
Enter a Float value: 25.09
You entered: 25.09

c. Print a Double value entered by an user in kotlin using Scanner.

Solution –

import java.util.Scanner

fun main(args: Array<String>) {

    // Create an object of Scanner class that is used to take input form standard input.
    val reader = Scanner(System.`in`)

    print("Enter a Double value: ")

    //nextDouble() method is used to take next double value from keyboard and store it in enteredValue variable.
    var enteredValue: Double = reader.nextDouble()

    println("You entered: $enteredValue")
}

Output:
Enter a Double value: 25.09
You entered: 25.09

d. Print a Double value entered by an user in kotlin without using Scanner.

Solution –

fun main(args: Array<String>) {

    print("Enter a Double value: ")

    Here, readLine() method takes input from standard input and !! ensures that value is not null
    val stringInput = readLine()!!

    // .toDouble() function converts the string input to double
    var doubleValue: Double = stringInput.toDouble()

    // prints the converted double value.
    println("You entered: $doubleValue")
}

Output:
Enter a Double value: 25.09
You entered: 25.09

e. Print a Boolean value entered by an user in kotlin using Scanner.

Solution –

import java.util.Scanner

fun main(args: Array<String>) {

    // Create an object of Scanner class that is used to take input form standard input.
    val reader = Scanner(System.`in`)

    print("Enter a Boolean value: ")

    //nextBoolean() method is used to take next Boolean value from keyboard and store it in enteredValue variable.
    var enteredValue: Boolean = reader.nextBoolean()

    println("You entered: $enteredValue")
}

Output:
Enter a Boolean value: false
You entered: false

f. Print a Boolean value entered by an user in kotlin without using Scanner.

Solution –

fun main(args: Array<String>) {

    print("Enter a Boolean value: ")

    Here, readLine() method takes input from standard input and !! ensures that value is not null
    val stringInput = readLine()!!

    // .toBoolean() function converts the string input to Boolean
    var booleanValue: Boolean = stringInput.toBoolean()

    // prints the converted Boolean value.
    println("You entered value: $booleanValue")
}

Output:
Enter a Boolean value: true
You entered: true

Similarly, you can do for values of other data type.




Conclusion

In this post, we have seen how to take input and output in kotlin programming language. Now, we know about print and println method. We know the difference between print and println method. We have also gone through readLine() method and Scanner class to take input from user. At last, we have seen different examples to show how to take input and output in kotlin.

Now, what next?
In the next post, you will learn about different operators used in kotlin. If you want you can also visit my previous post that explains different constants literals in kotlin.

Support Us

If you have any suggestion about this post, please comment below. We are looking for someone who helps use to make our tutorial better. Also, If you want tutorials on any topic, you can mail us at tutorialwing@gmail.com. We will try to cover the topic as soon as possible. Connect with us on Facebook, Google+, Twitter and Youtube for more updates.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners