Kotlin Input and Output Tutorial: Reading User Input, Printing Output, Formatting, and Mini Projects


This Kotlin Input and Output tutorial explains how to read user input from the console, print output, and format output effectively in Kotlin programs. The chapter also includes hands-on mini projects such as a calculator, number guessing game, student marks calculator, and temperature converter. Each topic is explained with clear examples and best practices to help beginners build confidence through practical coding.

Kotlin Input and Output – Complete Tutorial

Reading User Input

In Kotlin, user input is typically read from the standard input using readLine().

Example: Reading a String


print("Enter your name: ")
val name = readLine()
println("Hello, $name")

Reading and Converting Input

Since readLine() returns a nullable String, conversion is required for numbers.


print("Enter age: ")
val age = readLine()?.toInt()
println("Age is $age")

Safe Input Handling


val number = readLine()?.toIntOrNull() ?: 0

Best Practices

  1. Always handle null values from readLine().
  2. Use toIntOrNull() to avoid runtime exceptions.
  3. Validate user input wherever possible.

Printing Output

Kotlin provides simple functions to display output.

println

Prints output and moves to a new line.


println("Welcome to Kotlin")

print

Prints output without a new line.


print("Enter value: ")

Best Practices

  1. Use print for prompts.
  2. Use println for results and logs.

Output Formatting

Kotlin supports string templates for clean output formatting.

String Templates


val a = 10
val b = 20
println("Sum of $a and $b is ${a + b}")

Formatting with Decimal Precision


val price = 99.4567
println("Price: %.2f".format(price))

Best Practices

  1. Prefer string templates over string concatenation.
  2. Use formatting for financial or measurement values.

Mini Projects (With Examples)

Mini Project 1: Calculator

Objective

Create a simple calculator that performs basic arithmetic operations.

Code Example


fun main() {
print("Enter first number: ")
val a = readLine()?.toDoubleOrNull() ?: 0.0

print("Enter second number: ")
val b = readLine()?.toDoubleOrNull() ?: 0.0

print("Enter operator (+, -, *, /): ")
val op = readLine()

val result = when (op) {
"+" -> a + b
"-" -> a - b
"*" -> a * b
"/" -> if (b != 0.0) a / b else "Cannot divide by zero"
else -> "Invalid operator"
}

println("Result: $result")
}

Best Practices

  1. Validate operators.
  2. Handle division by zero.

Mini Project 2: Number Guessing Game

Objective

User guesses a predefined number.

Code Example


fun main() {
val secret = 7
print("Guess the number: ")
val guess = readLine()?.toIntOrNull()

if (guess == secret) {
println("Correct guess")
} else {
println("Wrong guess")
}
}

Best Practices

  1. Use loops for repeated attempts.
  2. Give hints for better user experience.

Mini Project 3: Student Marks Calculator

Objective

Calculate total and average marks.

Code Example


fun main() {
print("Enter marks for subject 1: ")
val m1 = readLine()?.toIntOrNull() ?: 0

print("Enter marks for subject 2: ")
val m2 = readLine()?.toIntOrNull() ?: 0

print("Enter marks for subject 3: ")
val m3 = readLine()?.toIntOrNull() ?: 0

val total = m1 + m2 + m3
val average = total / 3.0

println("Total Marks: $total")
println("Average: %.2f".format(average))
}

Best Practices

  1. Validate input range.
  2. Format output clearly.

Mini Project 4: Temperature Converter

Objective

Convert Celsius to Fahrenheit.

Code Example


fun main() {
print("Enter temperature in Celsius: ")
val celsius = readLine()?.toDoubleOrNull() ?: 0.0

val fahrenheit = (celsius * 9 / 5) + 32
println("Temperature in Fahrenheit: %.2f".format(fahrenheit))
}

Best Practices

  1. Use meaningful variable names.
  2. Display formatted output.

Summary

This chapter covered reading user input, printing and formatting output in Kotlin, followed by practical mini projects that strengthen understanding of basic syntax, control flow, and functions. These projects form a strong foundation for moving toward object-oriented programming and real-world applications.