0

I have an endless cycle going on. How do I stop the program to output the answer? The meaning of my program: reads all characters (including enter) and outputs the sum of only numbers.

fun main() {
fun StrToSum(str: String): Long {
    var sum : Long = 0
    var next = ""
    for (symbol in str + " ") {
        if (symbol == '-') {
            if ((next != "-") && (next != "")) {
                sum += next!!.toLong()
            }
            next = symbol.toString()
        } else if (symbol.isDigit()) {
            next += symbol
        } else if (next != "") {
            if (next != "-") {
                sum += next!!.toLong()
            }
            next = ""
        }
    }
    return sum
}

var string: String = ""

while (1<2) { //How stop it ?
    var str = readLine()!!.toString()
    string += " " + str
}
println (StrToSum(string)) //answer

} maybe there is some kind of keyboard shortcut ? I work for IntelliJ from Jetbrains

Sudar Kudr
  • 11
  • 1

1 Answers1

0

You can terminate the currently running program, but that will kill it - it won't be able to output the answer. You need to code that handling as part of your design, so you enable the user to finish and print your result.

The usual way people do this is to have some kind of cancel input, like entering an x or something:

// while (true) is a typical way to create an infinite loop
while (true) {
    var str = readLine()!!.toString()
    // look for the cancel token, break out of the loop if you see it
    if (str.lowercase() == "x") break
    string += " " + str
}

If you don't want to do that (remember you can make the cancel token anything, like the word "cancel" if you like, and put a prompt on the screen telling the user to type it to finish) then you'd have to do something like detecting other keycodes like Ctrl+Z or whatever - and I'm not sure how you'd do that from a basic command-line program reading from standard input. Maybe someone knows some tricks you could use! It's not something I've ever had to do, so I can't help you there


edit If you're happy to just look for control characters like ^D in the lines of standard input, you could do this kind of thing

if (str.firstOrNull().code == 4) break // char value for ^D

But that still requires the user to press Enter after the Ctrl+D, so the line including that character can be sent from the terminal to standard input. That's just how it works, outside of the solutions in the discussion I linked which involve OS-level interaction or building a GUI so you have access to the raw keypresses.

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • I'm sorry I answered late, but your method doesn't work, I need a keyboard shortcut ctrl+D – Sudar Kudr Sep 24 '22 at 19:41
  • @SudarKudr like I said, reading raw keypresses like that isn't something that standard input handles - usually you type a line into your console, press enter, and then that line is passed to your `Scanner`, your `readLine()` etc. There's a discussion about reading keystrokes in Java (and by extension in Kotlin) here: https://stackoverflow.com/q/1066318/13598222 I don't know much about it, but it looks like you either need to hook into the operating system's API using a library, or build yourself a GUI and grab the keystrokes from that – cactustictacs Sep 24 '22 at 19:59
  • @SudarKudr I've added an example for identifying control characters in your input - but that still requires the user pressing Enter after the Ctrl+D, because that's how your input in the terminal is sent to standard input for your app to consume – cactustictacs Sep 24 '22 at 21:20