Kotlin / Kotlin Basics

Handling Basic Input and Output in Kotlin

This tutorial will guide you through basic input and output operations in Kotlin. We will explore different ways to read input and display output.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Explores the foundational concepts in Kotlin, including variables, data types, and operators.

Introduction

In this tutorial, we aim to explore the basic input and output operations in Kotlin. Kotlin is a statically typed, cross-platform programming language that is concise, expressive, and designed to be safe. It's widely used for Android development, among other applications.

By the end of this tutorial, you will be able to:
- Read user input from the console in Kotlin.
- Display output to the console in Kotlin.

Prerequisites:
Familiarity with basic programming concepts is beneficial, but not necessary. If you are new to programming or Kotlin, you may need to refer to additional resources, but this tutorial aims to be as self-contained and beginner-friendly as possible.

Step-by-Step Guide

Input in Kotlin

To read the user's input in Kotlin, you use the readLine() function. This function reads a line of input from the console as a String?.

Output in Kotlin

To write output to the console in Kotlin, you use the print() or println() functions. print() displays the output without a newline at the end, whereas println() adds a newline at the end of the output.

Code Examples

Example 1: Reading Input and Writing Output

fun main() {
    print("Enter your name: ") // ask user for their name
    val name = readLine() // read the user's input
    println("Hello, $name!") // display a greeting
}

This code will ask the user to enter their name, read the user's input, and then display a greeting that includes the user's name.

Example 2: Converting Input to an Integer

fun main() {
    print("Enter your age: ") // ask user for their age
    val age = readLine()?.toInt() // read the user's input and convert it to an integer
    println("You are $age years old.") // display the user's age
}

This code will ask the user to enter their age, read the user's input, convert the input to an integer, and then display the user's age.

Summary

In this tutorial, we have learned how to handle basic input and output in Kotlin. You should now be able to read user input from the console and display output to the console in Kotlin.

Next, you might want to explore more advanced input/output topics, such as reading and writing files, or networking. There are also many other aspects of Kotlin that you might want to learn, such as its object-oriented features, or its support for functional programming.

Practice Exercises

  1. Write a program that asks the user to enter their name and their age, and then prints a message that tells the user how many years they have until they turn 100.

  2. Write a program that asks the user to enter a number, and then prints the square of that number.

  3. Write a program that asks the user to enter their height in inches, and then converts that height to centimeters and prints the result. (1 inch = 2.54 centimeters)

Solutions

  1. kotlin fun main() { print("Enter your name: ") val name = readLine() print("Enter your age: ") val age = readLine()?.toInt() val yearsUntil100 = 100 - age!! println("$name, you will turn 100 in $yearsUntil100 years.") }

  2. kotlin fun main() { print("Enter a number: ") val num = readLine()?.toDouble() val square = num!! * num println("The square of $num is $square.") }

  3. kotlin fun main() { print("Enter your height in inches: ") val heightInInches = readLine()?.toDouble() val heightInCm = heightInInches!! * 2.54 println("Your height in centimeters is $heightInCm.") }

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help