{"record":{"id":"0dd593b8f540de0f","repo":"TheAlgorithms/Java","slug":"must-be-a-natural-number","errorCode":null,"errorMessage":"Must be a natural number","messagePattern":"Must be a natural number","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/CollatzConjecture.java","lineNumber":32,"sourceCode":"     * @param n current number of the sequence\n     * @return next number of the sequence\n     */\n    public int nextNumber(final int n) {\n        if (n % 2 == 0) {\n            return n / 2;\n        }\n        return 3 * n + 1;\n    }\n\n    /**\n     * Calculate the Collatz sequence of any natural number.\n     *\n     * @param firstNumber starting number of the sequence\n     * @return sequence of the Collatz Conjecture\n     */\n    public List<Integer> collatzConjecture(int firstNumber) {\n        if (firstNumber < 1) {\n            throw new IllegalArgumentException(\"Must be a natural number\");\n        }\n        ArrayList<Integer> result = new ArrayList<>();\n        result.add(firstNumber);\n        while (firstNumber != 1) {\n            result.add(nextNumber(firstNumber));\n            firstNumber = nextNumber(firstNumber);\n        }\n        return result;\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/CollatzConjecture.java#L14-L43","documentation":"Thrown by CollatzConjecture.collatzConjecture when firstNumber < 1. The Collatz sequence is defined only over the natural numbers (positive integers): the rule halves even numbers and applies 3n+1 to odd numbers, terminating at 1. Zero and negative integers have no well-defined Collatz sequence, so the library rejects them before seeding the result list and the while loop.","triggerScenarios":"Calling collatzConjecture(0), collatzConjecture(-5), or passing a value parsed from input that was not bounds-checked. The check fires before any sequence generation, so it triggers on the very first call with a non-natural number.","commonSituations":"User-supplied or file-parsed integer not validated; off-by-one in a loop that starts at 0 and feeds the method; testing with edge cases without realising the domain is strictly positive.","solutions":["Pass a positive integer (>= 1) such as collatzConjecture(27).","Validate the input at the caller boundary: if (firstNumber < 1) reject with a user-facing message.","If parsing from a string, parse then range-check before invoking."],"exampleFix":"// before\nList<Integer> seq = cc.collatzConjecture(0);\n\n// after\nList<Integer> seq = cc.collatzConjecture(1);","handlingStrategy":"validation","validationCode":"if (firstNumber < 1) {\n    // reject with user-facing message\n    throw new IllegalArgumentException(\"Collatz start must be >= 1\");\n}\ncc.collatzConjecture(firstNumber);","typeGuard":"static boolean isNatural(int n) { return n >= 1; }","tryCatchPattern":null,"preventionTips":["Range-check all integer inputs parsed from strings or files before calling.","Start scan loops at 1, not 0, when feeding Collatz.","Validate at the application boundary, not deep inside processing."],"tags":["validation","number-theory","precondition","domain-error"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}