{"record":{"id":"88876a05552bb473","repo":"TheAlgorithms/Java","slug":"number-must-be-positive-88876a","errorCode":null,"errorMessage":"Number must be positive","messagePattern":"Number must be positive","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/KeithNumber.java","lineNumber":48,"sourceCode":"    /**\n     * Checks if a given number is a Keith number.\n     *\n     * <p>\n     * The algorithm works as follows:\n     * <ol>\n     * <li>Extract all digits of the number and store them in a list</li>\n     * <li>Generate subsequent terms by summing the last n digits</li>\n     * <li>Continue until a term equals or exceeds the original number</li>\n     * <li>If a term equals the number, it is a Keith number</li>\n     * </ol>\n     *\n     * @param number the number to check (must be positive)\n     * @return {@code true} if the number is a Keith number, {@code false} otherwise\n     * @throws IllegalArgumentException if the number is not positive\n     */\n    public static boolean isKeith(int number) {\n        if (number <= 0) {\n            throw new IllegalArgumentException(\"Number must be positive\");\n        }\n\n        // Extract digits and store them in the list\n        ArrayList<Integer> terms = new ArrayList<>();\n        int temp = number;\n        int digitCount = 0;\n\n        while (temp > 0) {\n            terms.add(temp % 10);\n            temp = temp / 10;\n            digitCount++;\n        }\n\n        // Reverse the list to get digits in correct order\n        Collections.reverse(terms);\n\n        // Generate subsequent terms in the sequence\n        int nextTerm = 0;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/KeithNumber.java#L30-L66","documentation":"Thrown by isKeith(int number) when the input is zero or negative. A Keith number is a number that appears in the Fibonacci-like sequence generated from its own digits (e.g., 14 generates 1,4,5,9,14). The digit-extraction loop (temp % 10 while temp > 0) would produce an empty sequence for non-positive inputs, making the algorithm undefined.","triggerScenarios":"Calling isKeith(0) or isKeith(-7). Common when iterating over a range that starts at 0 or when parsing fails and defaults to 0.","commonSituations":"Looping from i=0 in a search-for-Keith-numbers routine. Auto-unboxing an Integer that defaulted to 0. Off-by-one in a range generator that includes zero.","solutions":["Ensure the input is >= 1 before calling isKeith","Start search loops at 1 instead of 0","Use Optional or a default of 1 for parsed inputs that may be empty"],"exampleFix":"// before\nfor (int i = 0; i <= max; i++) {\n    if (isKeith(i)) print(i);\n}\n\n// after\nfor (int i = 1; i <= max; i++) {\n    if (isKeith(i)) print(i);\n}","handlingStrategy":"validation","validationCode":"if (number <= 0) {\n    throw new IllegalArgumentException(\"Input must be a positive integer: \" + number);\n}\nboolean result = KeithNumber.isKeith(number);","typeGuard":"static boolean isValidKeithInput(int number) {\n    return number > 0;\n}","tryCatchPattern":"try {\n    boolean result = KeithNumber.isKeith(number);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Invalid Keith number input: {}\", number);\n}","preventionTips":["Start search loops at 1, not 0, when looking for Keith numbers","Use Objects.requireNonNull and range checks on parsed user input before algorithm calls","Be aware that the method accepts int, not long — large values will overflow"],"tags":["math","validation","illegal-argument","number-theory"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}