Kotlin tutorial for Beginners – Kotlin Basic Input/Output

Kotlin Basic Input/Output

In this article, you will learn to display output to the screen, and take input from the user in Kotlin.

Koltin Output

You can use println() and print() functions to send output to the standard output (screen). Let’s take an example:

fun main(args : Array<String>) {
    println("Kotlin is interesting.")

When you run the program, the output will be:

Kotlin is interesting.

Here, println() outputs the string (inside quotes).

Difference Between println() and print()

  • print() – prints string inside the quotes.
  • println() – prints string inside the quotes similar like print() function. Then the cursor moves to the beginning of the next line.

When you use println() function, it calls System.out.println() function internally. (System.out.println() is used to print output to the screen in Java).

If you are using IntelliJ IDEA, put your mouse cursor next to println and go to Navigate > Declaration ( Shortcut: Ctrl + B . For Mac: Cmd + B), this will open Console.kt (declaration file). You can see that println() function is internally calling System.out.println().

Similarly, when you use print() function, it calls System.out.print() function.

Example 1: print() and println()

fun main(args : Array<String>) {
    println("1. println ");
    println("2. println ");

    print("1. print ");
    print("2. print");

When you run the program, the output will be:

1. println 
2. println 
1. print 2. print

Example 2: Print Variables and Literals

fun main(args : Array<String>) {
    val score = 12.3

    println("score = $score")
    println("${score + score}")

When you run the program, the output will be:

score = 12.3

Kotlin Input

In this section, you will learn to take input from the user..

To read a line of string in Kotlin, you can use readline() function.

Example 3: Print String Entered By the User

fun main(args: Array<String>) {
    print("Enter text: ")

    val stringInput = readLine()!!
    println("You entered: $stringInput")

When you run the program, the output will be:

Enter text: Hmm, interesting!
You entered: Hmm, interesting!

It’s possible to take input as a string using readLine() function, and convert it to values of other data type (like Int) explicitly.

If you want input of other data types, you can use Scanner object.

For that, you need to import Scanner class from Java standard library using:

import java.util.Scanner

Then, you need to create Scannerobject from this class.

val reader = Scanner(System.`in`)

Now, the reader object is used to take input from the user.

Example 4: Getting Integer Input from the User

import java.util.Scanner

fun main(args: Array<String>) {

    // Creates an instance which takes input from standard input (keyboard)
    val reader = Scanner(System.`in`)
    print("Enter a number: ")

    // nextInt() reads the next integer from the keyboard
    var integer:Int = reader.nextInt()

    println("You entered: $integer")

When you run the program, the output will be:

Enter a number: -12
You entered: -12

Here, reader object of Scanner class is created. Then, the nextInt() method is called which takes integer input from the user which is stored in variable integer.

To get LongFloatdouble and Boolean input from the user, you can use nextLong()nextFloat()nextDouble() and nextBoolean() methods respectively.


Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.