Skip to content

Checkpoint: Input Recovery

The Course
•
20 min study

This checkpoint combines text parsing, domain checks and continued processing. The goal is not merely to avoid a red error panel. It is to reject bad records honestly while preserving valid work and allowing later valid records to proceed.

The brief

The supplied String array contains duration texts. Assume every entry is a non-null String; null handling is outside this brief. For each entry, trim it and parse an integer. Accept values 1..240, add them to a total and count them. Skip malformed numeric text and out-of-range values. Print accepted count and total after processing all entries. This checkpoint uses an array so you can test processing independently of Scanner; the final project will connect line input.

Where recovery belongs

Decide where try/catch belongs: recovery applies separately to each entry, so a malformed element must not end the entire traversal. A catch after a try around the whole loop can skip everything after the first failure. Sketch the control flow before coding it.

Recover per element Java
public class Main {
    public static void main(String[] args) {
        String[] values = {"3", "bad", "4"};
        int total = 0;

        for (String value : values) {
            try {
                total += Integer.parseInt(value);
            } catch (NumberFormatException error) {
                System.out.println("Skipped one");
            }
        }

        System.out.println(total);
    }
}
Check your prediction
Text
Skipped one
7

The failed parsing expression never contributes a value to total. The catch belongs inside the loop so processing reaches the final 4. The checkpoint adds domain validation to this syntax-only example.

Count only after accepting a value Java
public class Main {
    public static void main(String[] args) {
        int[] values = {0, 5, 241};
        int accepted = 0;

        for (int value : values) {
            if (value >= 1 && value <= 240) {
                accepted++;
            }
        }

        System.out.println(accepted);
    }
}
Check your prediction
Text
1

The traversal has three iterations, but only one accepted record. Counting iterations would silently report bad input as real work. Keep the accepted update next to the successful domain check.

Plan your acceptance cases

Predict the accepted count and total for each row before you write the loop.

CaseExpected behaviorSkill it checks
{"25", "oops", "0", " 10 "}Accepted 2, total 35The main brief
{"bad", "5"}Accepted 1, total 5A later valid record still counts
{"240", "241"}Accepted 1, total 240Both ends of the 1–240 rule
empty arrayAccepted 0, total 0Empty input

When your version works, break it on purpose: move the try/catch so it wraps the whole loop. Which row now gives the wrong answer? Put it back.

Your turn

Exercise

Process the duration records

For {"25","oops","0"," 10 "}, print Accepted: 2 and Total: 35. Do not print a line for each rejected element in this brief. Continue after failures and do not catch unrelated programming errors.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        String[] records = {"25", "oops", "0", " 10 "};
        // Process records, then report accepted count and total.
    }
}
Hint 1: the idea

Keep parsing, validation and acceptance updates in that order. Each element gets its own attempt.

Hint 2: a first step

Declare counters before the loop. Put the specific catch inside the loop and update both counters only inside the valid-range branch.

Show one solution
Java
public class Main {
    public static void main(String[] args) {
        String[] records = {"25", "oops", "0", " 10 "};
        int accepted = 0;
        int total = 0;

        for (String record : records) {
            try {
                int minutes = Integer.parseInt(record.trim());

                if (minutes >= 1 && minutes <= 240) {
                    accepted++;
                    total += minutes;
                }
            } catch (NumberFormatException error) {
                // This record is rejected; the next iteration can still succeed.
            }
        }

        System.out.println("Accepted: " + accepted);
        System.out.println("Total: " + total);
    }
}
Text
Accepted: 2
Total: 35

The try sits inside the loop, so "oops" skips only itself. Both counters change only inside the range check, so "0" parses but adds nothing. The catch names NumberFormatException alone, so a real programming error would still stop the program instead of being counted as bad input.

Change it and explain

Run every acceptance case by replacing only the starting array. Then explain what your code promises about null elements: this brief excludes them, so it is not evidence that arbitrary null input is safe. A different boundary can reject null explicitly when its contract needs that guarantee.

Before Day 7, summarize the three kinds of outcome in your own words: valid and accepted, parsed but rejected by a rule, and malformed numeric syntax. The project's input handler will report these honestly while the model retains responsibility for its own invariants.

Was this article helpful?

Your response is saved on this device.