Unit 03 · lesson
Input Arrives as Data You Still Have to Interpret
A user typing 42 sees a number. A line-oriented input system first gives your program text.
Input becomes trustworthy only after interpretation
Receiving text, parsing a Java type, and validating a domain rule are separate boundaries.
- RAW TEXTreceive characters from an input boundaryparse
- PARSEattempt conversion into a Java typeproduce
- TYPED VALUEhold the converted representationvalidate
- DOMAIN RULEdecide whether the typed value is acceptableuse
- APPLICATION STATEonly validated meaning drives later behavior
Java 25's IO.readln makes that visible without forcing us into a larger input API immediately.
void main() {
String text = IO.readln("Enter a test count: ");
IO.println("You entered: " + text);
}
At this point text is a String. The characters might be 42, forty-two, an empty line, or something else.
Parsing is a claim
If the program needs an integer, it can attempt to parse:
int count = Integer.parseInt(text);
That line makes a claim:
text contains a valid representation of an int
If the claim is false, the program can compile perfectly and then fail during execution with a NumberFormatException.
That distinction matters. The syntax is valid. The type relationship is valid. The runtime data violates an expectation.
A small interactive program
Run:
void main() {
String name = IO.readln("Name: ");
String countText = IO.readln("Completed tasks: ");
int count = Integer.parseInt(countText);
IO.println(name + " completed " + count + " tasks.");
}
Test at least these inputs:
| name | count text | prediction |
|---|---|---|
| Maya | 7 | valid integer |
| Lee | 0 | valid integer |
| Noor | -2 | syntactically valid integer, maybe invalid for the domain |
| Alex | seven | parse failure |
| Sam | blank | parse failure |
Notice the difference between parseable and acceptable.
-2 can be parsed into an integer. But a negative count of completed tasks may violate the application's rules.
Parsing answers:
Can these characters become this Java type?
Validation answers:
Is this typed value allowed in this application?
Those are separate boundaries.
Do not hide invalid data yet
Later you will handle invalid input gracefully. For now, preserve the failure. Read the exception enough to identify:
- the exception type;
- the data that triggered it;
- the line responsible for interpreting the data.
Do not paste the entire stack trace into your evidence and call that analysis.
Build an input contract
For a fictional competition scoring tool, define inputs for:
- team name;
- match number;
- points scored;
- whether a penalty occurred.
For each input, write:
raw form received:
target Java type:
parse rule:
domain validation rule:
example valid value:
example parse failure:
example parseable but invalid value:
You have just designed an input boundary before writing the complete application.
That habit scales. Most real systems fail less often when assumptions about incoming data are visible instead of scattered through the code.