Kotlin, lesson 3
So far your programs have printed the same thing every time. Reading input lets a program
work with data you give it. In Kotlin the simplest tool is readln(): it reads one whole line
from standard input and gives it back as a String, without the line break at the end.
If there is no line left to read, readln() throws an error and the program stops. When you
expect that input might run out, use readlnOrNull() instead: it returns null at the end of
the input, which you will use in later lessons.
Input always arrives as text. The line "42" is two characters, not the number 42, so you
must convert it before doing maths:
toInt(), and throw an error if the text isn't a valid number (for
example ). Their cousins and return instead,
which lets you handle bad input gracefully.
2 exercises to try what you just read.
toLong()toDouble()"ten"toIntOrNull()toDoubleOrNull()nullOften a single line holds several values separated by spaces, like 3 4. Split the line into
pieces with split(" "), then convert each piece with map:
map { it.toInt() } runs toInt() on every piece (it is the current piece). The
val (width, height) = ... form unpacks the first two numbers into two names at once.
| Operator | Meaning | 17 op 5 |
|---|---|---|
+ | add | 22 |
- | subtract | 12 |
* | multiply | 85 |
/ | divide | 3 |
% | remainder | 2 |
Watch the division row. When both sides are whole numbers, / gives a whole number and
throws away the fractional part: 17 / 5 is 3, not 3.4. The % operator gives what is
left over, so 17 % 5 is 2. If either side is a Double, you get a decimal answer:
17 / 5.0 is 3.4. Convert first when you want a precise result: a.toDouble() / b.
Kotlin follows the usual maths order: *, / and % happen before + and -, and
parentheses override everything. 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20.
For a var, there are shortcuts that update it in place:
A group shares a restaurant bill. Everyone pays the same whole number of dollars, and whatever can't be split evenly is left over as the tip.
Input: the bill in whole dollars on the first line, the number of people on the second line (always at least 1).
Output: two lines, exactly in this form:
Sample input:
Sample output:
Read a temperature in degrees Celsius (it may have a decimal point) and print it in degrees
Fahrenheit, using F = C * 9 / 5 + 32. Just print the number with println.
Sample input:
Sample output:
Hint: read the input with toDouble() so the division keeps its decimals.
fun main() {
val name = readln()
println("Welcome, $name!")
}fun main() {
val apples = readln().toInt() // "42" -> 42
val weight = readln().toDouble() // "1.5" -> 1.5
println(apples * 2)
println(weight * 2)
}fun main() {
val (width, height) = readln().split(" ").map { it.toInt() }
println(width * height)
}fun main() {
var coins = 10
coins += 5 // same as coins = coins + 5 -> 15
coins -= 3 // 12
coins *= 2 // 24
coins++ // add one -> 25
println(coins)
}Each pays: <amount>
Tip: <left over>100
3Each pays: 33
Tip: 1100212.0