Skip to workspace
Kotlin Lab

Everyday Kotlin / Chapter 07

Format a transit ETA

Place identifiers, expressions, and literal suffixes in a string template.

The decision

The dispatch display accepts a compact wire label. Join the route, the exact suffix _ETA, whole minutes suffixed m, and remaining seconds suffixed s. Keep the route exactly as supplied.

Read the Kotlin

Locate the end of a template entry

After a dollar sign Kotlin reads a single identifier. A dot ends that entry, so a property name after the dot can become literal output. Braces open an expression and let the compiler evaluate the property access before inserting its value.

val city = "Lyon"
"$city.length"    // Lyon.length
"${city.length}"  // 4

Mark the boundary before a suffix

Letters, digits after the first character, and underscores can belong to the same identifier. If an underscore follows a variable in a template, Kotlin will look up the longer name. Use braces to mark exactly where the variable ends when the following characters belong to the display format.

val bus = "C"
val bus_id = "42"
"$bus_id / ${bus}_id"  // 42 / C_id

Evaluate arithmetic before formatting

In a braced entry Kotlin evaluates the expression with its normal numeric types, then turns the result into text. Two Int operands give integer division, and the remainder gives the seconds left over. Make one operand a Double and the quotient can contain a fraction, which changes the text a machine or a passenger sees.

val seconds = 135
"${seconds / 60}m ${seconds % 60}s"  // 2m 15s
"${seconds / 60.0}m"                 // 2.25m

The full example

Java 25
class Lesson {
    static String eta(String route, int seconds) {
        return route + "_ETA " + (seconds / 60) + "m " + (seconds % 60) + "s";
    }
}
Kotlin/JVM
fun eta(route: String, seconds: Int): String =
    "${route}_ETA ${seconds / 60}m ${seconds % 60}s"

Inputs and outcomes

InputOutcomeWhy it matters
Route R2, 125 secondsR2_ETA 2m 5sInteger quotient gives minutes and remainder gives seconds.
Route X, 60 secondsX_ETA 1m 0sEmit the zero remainder.
Route $A, 0 seconds$A_ETA 0m 0sContent inside a variable is inserted literally.

Walk through the code

  1. A simple template entry reads an identifier. Because an underscore is valid in an identifier, $route_ETA would look for a variable named route_ETA. Braces pin the boundary after route.
  2. Brace-delimited entries also accept arithmetic. Kotlin performs integer division before converting each result to text. The Java concatenation groups division first as well.
  3. Interpolation runs on the source template. A dollar sign supplied in the route data stays in the output.

The Java habit

Writing $seconds / 60 inside the quoted string inserts the number and leaves the slash and 60 as text. Put the division inside one brace-delimited expression.

When to choose differently

An end-user timetable usually needs localized units and plurals. Keep this literal template for the specified machine-readable label, and use localized message resources for translated UI.

References

Open quizTry code