Unit 14 · lesson
Separate Reading Bytes From Interpreting Records
Suppose a text file would contain:
2180,95,true
341,88,false
102,invalid,true
There are at least two different jobs:
- obtain lines from some source;
- interpret each line as a domain record.
Do not glue those responsibilities together immediately.
Browser-core parser
Use supplied lines directly:
record MatchRecord(int team, int score, boolean clean) {}
MatchRecord parseLine(String line) {
String[] parts = line.split(",");
if (parts.length != 3) {
throw new IllegalArgumentException("expected 3 fields");
}
int team = Integer.parseInt(parts[0].trim());
int score = Integer.parseInt(parts[1].trim());
boolean clean = Boolean.parseBoolean(parts[2].trim());
if (team <= 0 || score < 0) {
throw new IllegalArgumentException("invalid record values");
}
return new MatchRecord(team, score, clean);
}
Parsing has layers
Parsing structured text is a sequence of separate claims
A raw record can satisfy one layer and fail the next.
- RAW RECORDpreserve the supplied line and identitysplit
- FIELDScheck shape and expected field countconvert
- TYPED VALUESparse numeric and boolean representationsvalidate
- DOMAIN RULESreject values that violate application meaningconstruct
- RECORDcreate trusted domain state or preserve failure evidence
102,invalid,true has the correct field count but fails numeric conversion.
-4,90,true can convert to integers but violates a domain rule.
Those are different defects.
Boolean parsing has a trap
Boolean.parseBoolean(text) returns true only for case-insensitive "true"; other strings become false instead of throwing an exception.
If your data contract only allows exactly true or false, validate the raw token before trusting a silent false value.
For example, normalize and check membership explicitly.
This is a strong lesson in API contracts: a library method's behavior may not match your application's validation policy.
Parse a batch without losing record identity
Use:
var lines = List.of(
"2180,95,true",
"341,88,false",
"102,invalid,true"
);
Process by line number so a failure report can identify which supplied record failed.
Do not discard the line index/context and print only invalid data.
Evidence
Create a parser for a three-field record. Test:
- one valid ordinary record;
- one boundary-valid record;
- wrong field count;
- invalid numeric field;
- parseable but domain-invalid numeric field;
- invalid boolean token if your contract restricts it.
Preserve the raw line and the stage where it failed.
This is full parsing mastery even without local filesystem access.