{"record":{"id":"826c48f52c805423","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-826c48","errorCode":null,"errorMessage":"Input must be non-negative!","messagePattern":"Input must be non-negative!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/JacobsthalNumber.java","lineNumber":26,"sourceCode":" *\n * @see <a href=\"https://en.wikipedia.org/wiki/Jacobsthal_number\">\n *     Wikipedia: Jacobsthal Number</a>\n */\npublic final class JacobsthalNumber {\n\n    private JacobsthalNumber() {\n        // Utility class\n    }\n\n    /**\n     * Calculates the nth term of the Jacobsthal Sequence.\n     *\n     * @param n the index of the sequence (must be non-negative)\n     * @return the nth term of the Jacobsthal Sequence\n     */\n    public static long jacobsthal(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative!\");\n        }\n        if (n == 0) {\n            return 0;\n        }\n        if (n == 1) {\n            return 1;\n        }\n        long a = 0;\n        long b = 1;\n        long result = 0;\n        for (int i = 2; i <= n; i++) {\n            result = b + 2 * a;\n            a = b;\n            b = result;\n        }\n        return result;\n    }\n}","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/JacobsthalNumber.java#L8-L44","documentation":"Thrown by JacobsthalNumber.jacobsthal(int n) when n is negative. The Jacobsthal sequence is defined for non-negative indices (J(0)=0, J(1)=1, J(n)=J(n-1)+2*J(n-2)). The method uses an iterative loop from i=2 to n, so a negative n would skip the loop entirely but the guard prevents semantically invalid input. The error uses an exclamation mark ('non-negative!') which differs in style from other messages.","triggerScenarios":"Calling jacobsthal(-1), jacobsthal(-10), or any negative int. The check fires before the base-case returns (n==0 returns 0, n==1 returns 1).","commonSituations":"User input or computed indices that go negative. Off-by-one errors in loops that decrement past zero. Recursive or formula-based callers that produce negative indices.","solutions":["Ensure n >= 0 before calling jacobsthal().","Clamp or reject negative indices at the input boundary.","Wrap in try-catch(IllegalArgumentException) for defensive handling of untrusted input."],"exampleFix":"// before\nlong result = JacobsthalNumber.jacobsthal(n);\n\n// after\nif (n < 0) {\n    throw new IllegalArgumentException(\"Index must be non-negative: \" + n);\n}\nlong result = JacobsthalNumber.jacobsthal(n);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"Index must be non-negative: \" + n);\n}\nlong result = JacobsthalNumber.jacobsthal(n);","typeGuard":"static boolean isNonNegative(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    long result = JacobsthalNumber.jacobsthal(n);\n} catch (IllegalArgumentException e) {\n    // n was negative; handle invalid index\n}","preventionTips":["Ensure n >= 0 before calling jacobsthal().","Clamp or reject negative indices from user input.","Guard loop bounds that may decrement past zero."],"tags":["math","jacobsthal","sequence","argument-validation","non-negative"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}