Table of contents
1.
Introduction
2.
Kotlin Input
2.1.
Taking input using readline() method  
2.2.
Taking input using Scanner class
2.3.
Taking input without using the Scanner class
3.
Kotlin Output
3.1.
Difference between println() and print()
3.2.
Print literals and Variables 
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Input/Output

Author Vasu Bansal
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Input/output is a crucial part that makes any program interactive. Keyboards are the standard devices to send input to the program, and the output of the program is made visible with the help of another standard device known as Monitor.

So in this blog, we will discuss the basic input/output operations in Kotlin. An input is a stream of bytes/data that flows from the keyboard to the CPU. Similarly, the output is a stream of bytes that flows from the CPU towards the monitor.

Kotlin Input

We can take input from a user in a Kotlin program with the help of the following functions: 

→ readline() method
→ Scanner class

Taking input using readline() method  

// Kotlin program to demonstrate taking input from a user through readLine() function
fun main() {
    print("Enter input text: ")
    var input_text = readLine()
    print("The entered text is: $input_text")
}

Input:

Enter input text: Welcome to Coding Ninjas

Output:

The entered text is: Welcome to Coding Ninjas

 

Taking input using Scanner class

The readLine() function helps us take an input of only String data type. To take inputs of other data types, you can use the Scanner class. 

Import the Scanner at the top of the program as depicted below:

import java.util.Scanner

For taking input from the user, you need to create an object of the Scanner class, as shown in the program below. In this example we will take input as an integer, float and boolean using the nextInt(), nextFloat() and nextBoolean() functions respectively. Similarly, we can use nextLong() and nextDouble() methods to take long and double inputs, respectively.

import java.util.Scanner

// Kotlin program to demonstrate input/output operations
fun main() {
   
    // Create an object of scanner class
    val num1 = Scanner(System.`in`)      
    print("Enter an integer: ")
    // Using nextInt() method
    var num_1:Int = num1.nextInt()
    println("The entered value is: $num_1")
 

    val num2 = Scanner(System.`in`)
    print("Enter a float value: ")
    // Using nextFloat() method
    var num_2:Float = num2.nextFloat()
    println("The entered value is: $num_2")
 

    val boolval = Scanner(System.`in`)
    print("Enter a boolean: ")
    // Using nextBoolean() method
    var bool_val:Boolean = boolval.nextBoolean()
    println("The entered value is: $bool_val")
}

Input:

Enter an integer: 715
Enter a float value: 39.39
Enter a boolean: false

Output:

The entered value is: 715
The entered value is: 39.39
The entered value is: false

 

Taking input without using the Scanner class

There is another way to read an integer or double value without using the Scanner class. We can read the input as a String using readLine() function and then convert the String into an int or double using the toInt() and toDouble() functions. Also, to ensure a non-null value, the readLine() function is followed by two exclamation marks.

 

readline()!! - It ensures a non-null value is taken as input.

// Kotlin program to demonstrate reading input from the user and changing the data type
fun main() {
 
    print("Enter an Integer value: ")
    val input1 = readLine()!!  
    // Using toInt() function to convert String into Double
    var num: Int = input1.toInt()
    println("The entered value is: $num")


    print("Enter a double value: ")
    val input2= readLine()!!
    // Using toDouble() function to convert String into Double
    var double: Double = input2.toDouble()
    println("The entered value is: $double")
}

Input:

Enter an Integer value: 797
Enter a double value: 56.34

Output:

The entered value is: 797
The entered value is: 56.34

 

Kotlin Output

The following functions are generally used to display the output on the screen.

print() function
println() function

Difference between println() and print()

Both the print() and println() functions print the message inside the double quotes to the output screen. The main difference between the both is that the println() function moves the cursor to the beginning of the next line after printing the message, whereas the cursor stays in the same line after printing the message through the print() function. The following program depicts the difference between print() and println() functions:

// Kotlin program to demonstrate difference between print() and println() function
fun main(){
    println("Coding Ninjas - ")
    println("The best for learning Computer Science fundamentals")

    print("Coding Ninjas - ")
    print("The best for learning Computer Science fundamentals")      
}

Output:

Coding Ninjas - 
The best website for learning Computer Science fundamentals
Coding Ninjas - The best website for learning Computer Science fundamentals

Note: The print() functions internally calls System.out.print() whereas the println() function internally calls System.out.println(). 

 

Print literals and Variables 

The following program demonstrates how to take two variables as input and display their sum as output.

// A simple function that takes two integers as input and returns an integer which represents the sum of the two input integers.
fun sum(a: Int,b: Int) : Int{
    return a + b
}

// Kotlin program to read two numbers as input and display their result as output
fun main(){
    print("Enter a first integer: ")
    val input1 = readLine()!!  
    // Using toInt() function to convert String into Double
    var a: Int = input1.toInt()

    print("Enter a second integer: ")
    val input2 = readLine()!!  
    // Using toInt() function to convert String into Double
    var b: Int = input2.toInt()

    println("The sum of $a and $b is: ${sum(a,b)}")
}

Input:

Enter a first integer: 50
Enter a second integer: 30

Output:

The sum of 50 and 30 is: 80

Must Read Elvis Operator Kotlin

FAQs

  1. What is a double exclamation in Kotlin?
    It converts a value of nullable type into non-null type. For example s!!.length will throw a NullPointerException if s is null or return a non-null value of denoting the length of s if s is not null. 
     
  2. What does a question mark do in Kotlin?
    The question mark indicates that the arguments passed for these parameters can be null. For example, s?.length returns the length of s if s is not null. If s is null, it will return null.
     
  3. Is Kotlin a statically typed language?
    Yes, Kotlin is an open-source statically typed programming language.

Key Takeaways

Cheers if you reached here!! 

The purpose of this article was to introduce you to the basic input/output operations in the Kotlin programming language. Go ahead and try making your own interactive programs in Kotlin and change the world.  

Yet learning never stops, and there is a lot more to learn. So head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build the future applications. Till then, Happy Learning!!

Live masterclass