{"record":{"id":"a4da33e3d98042e5","repo":"TheAlgorithms/Java","slug":"number-must-be-non-negative","errorCode":null,"errorMessage":"Number must be non-negative.","messagePattern":"Number must be non-negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/LowestBasePalindrome.java","lineNumber":58,"sourceCode":"     *\n     * @param base the base to be checked\n     * @throws IllegalArgumentException if the base is less than or equal to 1\n     */\n    private static void checkBase(int base) {\n        if (base <= 1) {\n            throw new IllegalArgumentException(\"Base must be greater than 1.\");\n        }\n    }\n\n    /**\n     * Validates the number, ensuring it is non-negative.\n     *\n     * @param number the number to be checked\n     * @throws IllegalArgumentException if the number is negative\n     */\n    private static void checkNumber(int number) {\n        if (number < 0) {\n            throw new IllegalArgumentException(\"Number must be non-negative.\");\n        }\n    }\n\n    /**\n     * Computes the digits of a given number in a specified base.\n     * <p>\n     * The digits are returned in reverse order (least significant digit first).\n     * For example, the number 13 in base 2 produces [1,0,1,1] representing 1101 in\n     * binary.\n     * </p>\n     *\n     * @param number the number to be converted (must be non-negative)\n     * @param base   the base to be used for the conversion (must be greater than 1)\n     * @return a list of digits representing the number in the given base, with the\n     *         least significant digit at the beginning of the list\n     * @throws IllegalArgumentException if the number is negative or the base is\n     *                                  less than 2\n     */","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/LowestBasePalindrome.java#L40-L76","documentation":"Thrown by the private checkNumber(int number) method, invoked by computeDigitsInBase, isPalindromicInBase, and lowestBasePalindrome. The digit-extraction algorithm uses number % base and number / base, which behave differently for negative numbers in Java (sign-preserving remainder), producing incorrect digit lists. The guard rejects negatives to prevent silent wrong results.","triggerScenarios":"Calling LowestBasePalindrome.lowestBasePalindrome(-5), computeDigitsInBase(-10, 2), or isPalindromicInBase(-3, 2). Any public method in the class called with a negative number.","commonSituations":"Passing a signed difference or unvalidated user input. Processing data where negative sentinel values represent missing entries.","solutions":["Validate that number >= 0 before calling any LowestBasePalindrome method","Reject negative inputs at the ingestion boundary","Use Math.abs() only if the absolute value is meaningful for your use case"],"exampleFix":"// before\nint base = LowestBasePalindrome.lowestBasePalindrome(userNumber);\n\n// after\nif (userNumber < 0) throw new IllegalArgumentException(\"Number must be non-negative\");\nint base = LowestBasePalindrome.lowestBasePalindrome(userNumber);","handlingStrategy":"validation","validationCode":"if (number < 0) {\n    throw new IllegalArgumentException(\"Number must be non-negative: \" + number);\n}\nint base = LowestBasePalindrome.lowestBasePalindrome(number);","typeGuard":"static boolean isValidPalindromeInput(int number) {\n    return number >= 0;\n}","tryCatchPattern":null,"preventionTips":["Filter out negative sentinel values from data before passing to LowestBasePalindrome","All three public methods (computeDigitsInBase, isPalindromicInBase, lowestBasePalindrome) enforce this guard","Test with boundary values: 0, 1, and negative inputs"],"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"}