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.
Acceptance cases
For {“25”,“oops”,“0”,” 10 ”}, accept two values totaling 35. Empty input accepts zero and totals zero. For {“bad”,“5”}, the later valid value must still be counted. For {“240”,“241”}, accept exactly 240. These cases catch an early return, an unconditional addition and a missing upper-bound check.
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.
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
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.
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
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.
Your turn
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.
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
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);
}
}Accepted: 2
Total: 35 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.
Completion is saved in this browser only. It does not measure mastery.