Chapter 5: Input & Output Basics

Input & Output Basics in Kotlin — this is the chapter where your programs stop being silent and start talking to the user and working with real files! ☕🚀

We’re going to cover everything very slowly and clearly, like I’m sitting next to you in a quiet Bandra café, showing you every line on my laptop. We’ll use lots of real-life analogies, complete runnable examples, step-by-step explanations, common mistakes with fixes, and fun tips so it all sticks perfectly.

Let’s dive in!

1. Reading Input from User – readln() (The Modern Kotlin Way)

In Kotlin, the easiest and most recommended way to read input from the console is readln() (introduced in Kotlin 1.6+ — now standard in 2026).

readln() = read line → reads one full line of text from the user (including spaces) and returns it as String.

Basic example – Ask for name

Kotlin

Run it → IntelliJ shows a console → type your name → press Enter → magic!

What happens if user just presses Enter?

  • readln() returns an empty string (“”)
  • Very safe and simple — no Scanner hell like Java!

Another example – Ask for age (convert to Int)

Kotlin

Important: readln() returns String → you must convert it to Int, Double, etc. yourself. Use toIntOrNull(), toDoubleOrNull(), etc. → they return null if conversion fails (super safe!)

2. Old Way: readLine() (Still Works, but Avoid)

Before Kotlin 1.6, we used readLine() — it’s exactly the same as readln(), just older name.

Kotlin

Modern recommendation (2026): Always use readln() — it’s clearer and future-proof.

3. Output – println() vs print()

Function What it does New line? Example
println() Prints + adds newline (\n) Yes println(“Hello”) → Hello + new line
print() Prints without newline No print(“Hi”) → Hi (cursor stays on same line)

Examples

Kotlin

Output:

text

4. Beautiful String Formatting in Kotlin

Kotlin gives you three powerful ways to format strings:

Way 1: String Templates (Most common & beautiful)

Kotlin

Way 2: String.format() (like Java)

Kotlin

Way 3: String interpolation with variables (same as templates)

Kotlin

5. Simple File Reading & Writing (Intro to java.io + Kotlin Extensions)

Kotlin makes file I/O super simple with extension functions on File and Path.

Reading a Text File – One Line!

Kotlin

Even shorter (Kotlin idiomatic):

Kotlin

Writing to a File – Super Easy

Kotlin

Even nicer with use{} (auto close):

Kotlin

6. Quick Recap Table (Your Cheat Sheet)

Feature Kotlin Way (Best Practice) Notes / Example
Read user input readln() val name = readln()
Print with newline println(“Hello”) Adds \n automatically
Print without newline print(“Hi”) Cursor stays on same line
String template “Hello $name!” or ${age + 1} Most loved feature!
Raw multi-line “”” multi-line text “”” No escape needed
Read file File(“data.txt”).readText() or readLines() Very simple
Write file File(“out.txt”).writeText(“…”) Overwrites
Append to file File(“log.txt”).appendText(“…”) Adds to end

7. Common Newbie Mistakes & Fixes

Mistake Problem Fix
Using readLine() instead of readln() Old style – works but not idiomatic Use readln() (new name)
Forgetting to convert input to Int val age = readln() → String Use readln().toIntOrNull()
Writing ; after every line Looks like Java – not needed Remove most semicolons
Not handling empty input readln() returns “” if user just presses Enter Check if (input.isBlank()) { … }
Writing to file without try-catch IOException crashes Use try-catch or use {}

8. Homework for You (Let’s Make It Fun!)

  1. Basic Ask user for name and city using readln(). Print: “नमस्ते $name! तुम्ही $city मध्ये राहता!”
  2. Medium Ask user for two numbers → convert to Int safely → print their sum, difference, product.
  3. Advanced Ask user for email → print length using safe call + Elvis (if null → “No email provided”)
  4. Fun Create a file greeting.txt and write 3 lines using string templates (include current date).
  5. Challenge Read lines from a file names.txt → print only names longer than 5 characters.

You’ve just unlocked Kotlin’s beautiful I/O — your programs now talk to users and work with files like a pro!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *