Handling Basic Input and Output in Kotlin

Tutorial 5 of 5

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.") }