Reading console input in Kotlin

Note that since Kotlin 1.6 readLine()!! should be replaced with readln(). Here are A+B examples in Kotlin reading from stdin: fun main() { val (a, b) = readLine()!!.split(‘ ‘) println(a.toInt() + b.toInt()) } or fun main(vararg args: String) { val (a, b) = readLine()!!.split(‘ ‘).map(String::toInt) println(a + b) } or fun readInts() = readLine()!!.split(‘ ‘).map … Read more

How to read an integer input from the user in Rust 1.0?

Here is a version with all optional type annotations and error handling which may be useful for beginners like me: use std::io; fn main() { let mut input_text = String::new(); io::stdin() .read_line(&mut input_text) .expect(“failed to read from stdin”); let trimmed = input_text.trim(); match trimmed.parse::<u32>() { Ok(i) => println!(“your integer input: {}”, i), Err(..) => println!(“this … Read more

How to remove trailing and leading whitespace for user-provided input in a batch file?

The solution below works very well for me. Only 4 lines and works with most (all?) characters. Solution: :Trim SetLocal EnableDelayedExpansion set Params=%* for /f “tokens=1*” %%a in (“!Params!”) do EndLocal & set %1=%%b exit /b Test: @echo off call :Test1 & call :Test2 & call :Test3 & exit /b :Trim SetLocal EnableDelayedExpansion set Params=%* … Read more

nodejs how to read keystrokes from stdin

For those finding this answer since this capability was stripped from tty, here’s how to get a raw character stream from stdin: var stdin = process.stdin; // without this, we would only get streams once enter is pressed stdin.setRawMode( true ); // resume stdin in the parent process (node app won’t quit all by itself … Read more

What is the recommended way to make a numeric TextField in JavaFX?

Very old thread, but this seems neater and strips out non-numeric characters if pasted. // force the field to be numeric only textField.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (!newValue.matches(“\\d*”)) { textField.setText(newValue.replaceAll(“[^\\d]”, “”)); } } });