Common Bugs with Input / Output

📌 Common bugs · input-output✍️ Written by Tom Reyes📅 Reviewed 2026-04-11⏱ ~7 min read

I/O bugs are the costliest because they cross trust boundaries. These five hit beginners hardest.

Bee mascot
From Bee: "I see this exact bug in beginners every week. Read it once, and you'll spot it on yourself before it bites."
#1

Trusting form input as a number

js
const age = document.querySelector("#age").value;
if (age > 18) { ... }   // "100" > 18 is false (string compare)
⚠️
Why this happens
Form values are always strings. String comparison is alphabetical, not numeric.
The fix
Always parse: const age = Number(input.value). Validate !Number.isNaN(age).
#2

Forgetting to close a file

py
f = open("data.txt")
data = f.read()
# never closed
⚠️
Why this happens
Open file handles leak. After enough leaks, the OS refuses new opens.
The fix
with open("data.txt") as f: in Python; try-with-resources in Java; auto-handled in modern JS APIs.
#3

Reading huge files all at once

py
data = open("10gb.csv").read()
# crashes — out of memory
⚠️
Why this happens
Loading 10GB into a string blows your RAM.
The fix
Stream line-by-line: for line in f: .... Or use a CSV reader that streams.
#4

SQL injection via string concatenation

js
const sql = `SELECT * FROM users WHERE name = '${userInput}'`;
// disaster waiting
⚠️
Why this happens
User input becomes part of the query. Bobby Tables ruins your weekend.
The fix
Use parameterized queries: db.query("SELECT ... WHERE name = ?", [name]).
#5

Confusing stdout and stderr

sh
node script.js > log.txt   # only stdout saved
# errors disappear
⚠️
Why this happens
Errors go to stderr, not stdout. Default redirect captures only stdout.
The fix
2>&1 redirects stderr too: node script.js > log.txt 2>&1.
💡
Want the full lesson?

Read Input Output — the complete lesson, or test yourself with the quiz.

T
Tom Reyes
Reviewer · 12 yrs Java/JVM

Tom spent eight years as a backend engineer in fintech (Java + Kotlin) and four as a lead at an enterprise SaaS company. He reviews every Java example on this site and writes the data-structure deep dives. He cares deeply that beginners aren't taught bad habits they'll have to unlearn. More about Tom →