Java: Validate records with your own exception

Separate checking from reporting: a parse method throws a checked exception, and the caller decides what to print.

What to build

A program that parses name,age,email records and reports each one.

Requirements

  • Write a checked InvalidRecordException and a method parse(String line) that returns a valid record or throws it with the first problem, checked in this order: not exactly 3 comma-separated parts (expected 3 fields), an empty name after trimming (name is empty), an age that is not a whole number (age is not a number), an age outside 0 to 150 (age out of range), an email without exactly one @ (bad email).
  • For each line print ok: NAME (AGE) or line K: MESSAGE (lines numbered from 1). Finally print valid: V of N.

Input

Line 1: N. Then N lines.

Output

N lines, then the count.

Constraints

1 ≤ N ≤ 50. Trim spaces around each part.

Example

Input:

3
Ada, 36, ada@example.com
Alan,forty,alan@example.com
, 20, x@y

Output:

ok: Ada (36)
line 2: age is not a number
line 3: name is empty
valid: 1 of 3

Why it works

The checked exception forces every caller of parse to handle bad records, and the message carries the reason, so parsing and reporting stay separate.

Examples

Example 1 · Input
3
Ada, 36, ada@example.com
Alan,forty,alan@example.com
, 20, x@y
Expected output
ok: Ada (36)
line 2: age is not a number
line 3: name is empty
valid: 1 of 3
Example 2 · Input
2
Grace,85
Rina,200,rina@x.org
Expected output
line 1: expected 3 fields
line 2: age out of range
valid: 0 of 2

Hints

Hint 1

class InvalidRecordException extends Exception, thrown by a parse method with a clear message.

Hint 2

Check the parts in the order the requirements give, and stop at the first problem.

Solution

Show one correct solution (try it yourself first)
import java.util.Scanner;

public class Main {
    static class InvalidRecordException extends Exception {
        InvalidRecordException(String message) {
            super(message);
        }
    }

    static String parse(String line) throws InvalidRecordException {
        String[] parts = line.split(",", -1);
        if (parts.length != 3) throw new InvalidRecordException("expected 3 fields");
        String name = parts[0].trim();
        if (name.isEmpty()) throw new InvalidRecordException("name is empty");
        int age;
        try {
            age = Integer.parseInt(parts[1].trim());
        } catch (NumberFormatException e) {
            throw new InvalidRecordException("age is not a number");
        }
        if (age < 0 || age > 150) throw new InvalidRecordException("age out of range");
        String email = parts[2].trim();
        if (email.indexOf('@') < 0 || email.indexOf('@') != email.lastIndexOf('@')) throw new InvalidRecordException("bad email");
        return name + " (" + age + ")";
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine().trim());
        int valid = 0;
        for (int i = 1; i <= n; i++) {
            String line = in.nextLine();
            try {
                System.out.println("ok: " + parse(line));
                valid++;
            } catch (InvalidRecordException e) {
                System.out.println("line " + i + ": " + e.getMessage());
            }
        }
        System.out.println("valid: " + valid + " of " + n);
    }
}

Lesson: Exceptions →