Jitendar M
Jitendar M

Reputation: 643

Reading input in kotlin using generate sequence

I'm a newbie in kotlin.

I was running the below code to understand how BigDecimal works, but i'm missing something on how to safely exit from intellij terminal after entering the input. I always get NumberFormatException if i don't enter anything and press Enter key. How to quit safely and see the result.

import java.math.BigDecimal

fun main(args: Array<String>) {
    val input = generateSequence { readLine() }
    println(input
        .map { it.toBigDecimal() }
        .fold(0.toBigDecimal()) { a, b -> a + b })
}

Source https://medium.com/@elizarov/floating-point-for-decimals-fc2861898455

Upvotes: 0

Views: 443

Answers (1)

Andrei Tanana
Andrei Tanana

Reputation: 8442

You have to press Ctrl+d after input

Upvotes: 2

Related Questions