{"record":{"id":"d7e24bd6d7fef232","repo":"TheAlgorithms/Java","slug":"input-n-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"Input 'n' must be a non-negative integer.","messagePattern":"Input 'n' must be a non-negative integer\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FibonacciLoop.java","lineNumber":34,"sourceCode":" * <li>{@link com.thealgorithms.matrix.matrixexponentiation.Fibonacci} - O(log n) Matrix Exponentiation approach</li>\n * </ul>\n */\npublic final class FibonacciLoop {\n\n    private FibonacciLoop() {\n        // Private constructor to prevent instantiation of this utility class.\n    }\n\n    /**\n     * Calculates the nth Fibonacci number.\n     *\n     * @param n The index of the Fibonacci number to calculate.\n     * @return The nth Fibonacci number as a BigInteger.\n     * @throws IllegalArgumentException if the input 'n' is a negative integer.\n     */\n    public static BigInteger compute(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input 'n' must be a non-negative integer.\");\n        }\n\n        if (n <= 1) {\n            return BigInteger.valueOf(n);\n        }\n\n        BigInteger prev = BigInteger.ZERO;\n        BigInteger current = BigInteger.ONE;\n\n        for (int i = 2; i <= n; i++) {\n            BigInteger next = prev.add(current);\n            prev = current;\n            current = next;\n        }\n\n        return current;\n    }\n}","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FibonacciLoop.java#L16-L52","documentation":"Thrown by FibonacciLoop.compute when n < 0. The method computes the nth Fibonacci number iteratively; the sequence F(0)=0, F(1)=1 is defined for non-negative indices only, and the for loop `for (i = 2; i <= n; i++)` would not execute for negative n (silently returning via the n <= 1 branch with a wrong sign), so the guard rejects negatives explicitly.","triggerScenarios":"Calling compute(-1) or any negative n. Common when n is derived from a subtraction or parsed from unvalidated input.","commonSituations":"n computed as a difference that underflows; user input not bounded; loop boundaries that dip below zero; off-by-one in index arithmetic.","solutions":["Pass a non-negative int n (>= 0); compute(0) returns 0, compute(1) returns 1.","Validate at the caller: if (n < 0) reject before calling.","Clamp derived indices with Math.max(0, n)."],"exampleFix":"// before\nBigInteger f = FibonacciLoop.compute(rank - 1); // rank == 0 => -1\n\n// after\nBigInteger f = FibonacciLoop.compute(Math.max(0, rank - 1));","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"Fibonacci index must be >= 0\");\n}\nFibonacciLoop.compute(n);","typeGuard":"static boolean isNonNegative(int n) { return n >= 0; }","tryCatchPattern":null,"preventionTips":["Guard derived indices like compute(rank - 1) with Math.max(0, rank - 1).","Validate parsed integers before calling.","Remember compute(0) is valid and returns 0."],"tags":["validation","sequences","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}