How do I read input from the user?
← Getting Started · Ref: Q4
Use the built-in Stdin type:
stdin <- Stdin()
Read a single line with a guard (only executes if input is available):
if line <- stdin.next() stdout.println(`You entered: ${line}`)
Iterate over all lines until EOF:
while stdin? stdout.println(stdin.next())
In a stream pipeline, stdin is a source:
cat stdin | filter by notEmpty > stdout
Stdin implements the StringInput trait with these methods:
hasNext() check if more input is available next() get the next line as String ? isSet operator, true when input available
Stdin works with both interactive input and piped data:
echo "Hello" | ek9 myProgram.ek9 cat data.txt | ./myProgram.ek9 ls -la | ek9 myProgram.ek9
The same program handles all three cases without any changes.
When piped input ends or the user sends EOF (Ctrl+D on Unix, Ctrl+Z on Windows), hasNext() returns false.
For the full Stdin API, run: ek9 -h Stdin
See Q3 for printing output. See Q37 for string operations on input lines.
Example
defines module qa.getting.started.read.input defines program ReadInput() stdin <- Stdin() stdout <- Stdout() stdout.println("Type something and press Enter:") if line <- stdin.next() stdout.println(`You said: ${line}`)
Common mistakes
E50060 — EK9 Stdin uses next() to read the next line, not readLine(). AI often uses Java-style method names. Triggers E50060 — method not resolved. See ek9 -h Stdin for the full API.
Incorrect:
if line <- stdin.readLine()
Correct:
if line <- stdin.next()
E08020 — Declaring stdin without initialising it (as Stdin?) then calling stdin.next() uses an uninitialised variable as a method receiver. Triggers E08020 — might be used before being initialised. Always initialise with <- Stdin(). See ek9 -h E08020 for details.
Incorrect:
stdin as Stdin?
Correct:
stdin <- Stdin()
Other ways to ask this
- How to read from stdin in EK9
- EK9 equivalent of input or scanf
- How to get user input in EK9
Coming from another language?
Python: input(), Java: Scanner(System.in), Rust: stdin().read_line(), Go: fmt.Scanln() or bufio.NewReader. EK9 uses a guard pattern with Stdin — the compiler ensures you only process input when it is available.
Keywords: beginner, console, keyboard, first, user, pipe, start, interactive, read, stdin, readline, intro, input