Terminology (2)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Line delimiter
- The character or sequence that separates lines. The selected reader decides whether it remains in the returned text.
- Numeric prefix
- The initial part of a string that matches a numeric grammar, before the first nonmatching character.
Reading text and printing values
An input reader turns incoming characters into something the program can inspect, such as a line of text. Parsing or conversion interprets that text as a value of another type. Formatting chooses the characters used to display a value. These are separate steps, each with its own rules.
For example, a width field can arrive as text, become a numeric measurement for a calculation and return to text with a unit label. Decide which original characters must be retained, which whitespace belongs to the input boundary and what counts as a complete conversion. Then check the selected reader, converter and printer against those decisions.
01Empty strings
Does empty text become a number?
A print studio uses a command-line tool to preview proof labels. The proof number and measured width already print; you are adding an entered count. An empty field must stay distinguishable from a written zero, and the preview must show the count the operator actually entered.
First decide what the count helper will receive when the field is left blank. This small
probe keeps input reading out of the way: its string contains a space, a tab and CRLF.
Each example trims that string, calls a numeric converter and prints the result or refused. Will checking conversion alone keep a blank entry distinct from
zero?
const raw = " \t\r\n";
const text = raw.trim();
const count = Number(text);
if (Number.isInteger(count)) {
console.log(count);
} else {
console.log("refused");
}
02Line endings
Who removes the newline?
The blank probe supplied its own string. The tool will receive stdin, so now inspect what
actually reaches the count helper when the operator enters zero and presses Enter. Replace
the main file with this reader example and pipe in 0\n. The quoted output
exposes the returned text. Use the workspace from the previous reading; these examples
need only the standard libraries.
The choice of API matters: these examples use Node readline, Go ReadString, Rust read_line and C++ getline. Another line API in the same language may make a different choice about the delimiter.
import { createInterface } from "node:readline";
import { stdin } from "node:process";
const lines = createInterface({ input: stdin, crlfDelay: Infinity });
for await (const line of lines) {
console.log(JSON.stringify(line));
break;
}
printf '0\n' | node main.ts
03Numeric prefixes
Where does conversion stop?
The helper can now separate blank text from zero and account for the reader's line ending.
Try a mistyped count next: 12x. The preview should flag that entry instead of
quietly displaying twelve. Return to the first conversion probe and replace its raw
fixture with 12x. Does this converter reject it, or return a number from its
beginning?
04Printing
Spaces, newlines and numeric formatting
With a count ready to display, check the whole preview line. These programs use default
formatting for the values 7, 12.5 and 0. They print proof 7 | width=12.5 cm | count=0, followed by LF.
const id = 7,
width = 12.5,
count = 0;
const record = `proof ${id} | width=${width} cm | count=${count}`;
console.log(record);
The studio's label layout requires proof 007 | width=12.50 cm | count=0. The
proof number needs a minimum width: padding must not truncate a longer identifier. The
measured width needs two fractional digits, even when one is zero. These are separate
formatting choices. Use the selected language's operations below to change this preview.
console.log separates multiple arguments with spaces and adds a newline. Here the complete string supplies its own separators. padStart pads short identifiers without truncating longer ones; toFixed(2) retains two fractional digits.
The examples write their result to stdout. Diagnostic output has its own stderr APIs; keep it separate when another program reads the printed result.
05Try it
Keep the entry visible as input grows
Before extending the tool, try the reader with 0\n, 0 followed by
EOF, and a blank line. Pass the returned text to the converter before and after trimming. Check
that the preview can preserve a written zero and a final count without Enter.
Then change the formatter's proof number to 1234 and width to 0.5. Keep the whole identifier and print the width with its trailing zero. The lab begins with this layout work, then adds the count helper to the same program. Each level supplies the earlier working code.
The final extension previews several count entries for the same proof, one per input line. Beside each result, show the original entry in quotes so an operator can inspect spaces, leading zeroes or refused text. Trimming that displayed entry would hide evidence that the preview is meant to show.
This version uses a supplied stdin adapter that delivers short text fragments. The line
readers above assembled complete lines for you; this adapter leaves that work to your
code. To exercise the boundary, it delivers at most three characters at a time. It can
send [" 4", "2\r", "\n9"]. Together these contain the line " 42", terminated by CRLF, and the final line "9" at EOF. Converting
each fragment separately would turn one count into several. Assemble the line before conversion,
remove its actual LF or CRLF ending, and retain the remaining text for display. A lone CR is
content. At EOF, keep final text without inventing an extra entry after a terminating newline.
The earlier converter still trims its own copy.
Printing and input practice
Finish the proof-label preview: repair its layout, add one entered count, then preview several entries while preserving their original text across input fragments.
Check this idea later
Sources
API references checked 30 October 2026.
Sign in to keep your read marks on every device. Sign in
Was this helpful?