{"record":{"id":"e4b453ba9b844916","repo":"TheAlgorithms/Java","slug":"given-range-of-values-is-invalid","errorCode":null,"errorMessage":"Given range of values is invalid!","messagePattern":"Given range of values is invalid!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/AmicableNumber.java","lineNumber":34,"sourceCode":" * link: https://en.wikipedia.org/wiki/Amicable_numbers\n * <p>\n * Simple Example: (220, 284)\n * 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110} <-SUM = 284\n * 284 is divisible by {1,2,4,71,142} <-SUM = 220.\n */\npublic final class AmicableNumber {\n    private AmicableNumber() {\n    }\n    /**\n     * Finds all the amicable numbers in a given range.\n     *\n     * @param from range start value\n     * @param to   range end value (inclusive)\n     * @return list with amicable numbers found in given range.\n     */\n    public static Set<Pair<Integer, Integer>> findAllInRange(int from, int to) {\n        if (from <= 0 || to <= 0 || to < from) {\n            throw new IllegalArgumentException(\"Given range of values is invalid!\");\n        }\n\n        Set<Pair<Integer, Integer>> result = new LinkedHashSet<>();\n\n        for (int i = from; i < to; i++) {\n            for (int j = i + 1; j <= to; j++) {\n                if (isAmicableNumber(i, j)) {\n                    result.add(Pair.of(i, j));\n                }\n            }\n        }\n        return result;\n    }\n\n    /**\n     * Checks whether 2 numbers are AmicableNumbers or not.\n     */\n    public static boolean isAmicableNumber(int a, int b) {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/AmicableNumber.java#L16-L52","documentation":"Thrown by AmicableNumber.findAllInRange when the range is structurally invalid: from<=0 OR to<=0 OR to<from. Amicable numbers are defined on positive integers and the search is a nested loop from i..to, so a non-positive endpoint or an inverted range is rejected before any work.","triggerScenarios":"findAllInRange(0, 10); findAllInRange(-3, 5); findAllInRange(10, 5) (inverted); findAllInRange where both bounds come from user input with no ordering check.","commonSituations":"UI range inputs where the user left 'from' blank (parses to 0); swapping min/max in the caller; inclusive-vs-exclusive bound confusion producing to<from.","solutions":["Supply a valid positive, ordered range: findAllInRange(1, 1000).","Normalize bounds before calling: lo=min(a,b), hi=max(a,b), then reject if lo<=0.","Validate user input and show a field error instead of forwarding to the API."],"exampleFix":"// before\nSet<?> r = AmicableNumber.findAllInRange(start, end);\n// after\nint lo = Math.min(start, end), hi = Math.max(start, end);\nif (lo <= 0) throw new IllegalArgumentException(\"range start must be positive\");\nSet<?> r = AmicableNumber.findAllInRange(lo, hi);","handlingStrategy":"validation","validationCode":"int lo = Math.min(from, to), hi = Math.max(from, to);\nif (lo <= 0) {\n    throw new IllegalArgumentException(\"range must be within positive integers\");\n}\nSet<?> r = AmicableNumber.findAllInRange(lo, hi);","typeGuard":"static boolean isValidRange(int from, int to) {\n    return from > 0 && to > 0 && from <= to;\n}","tryCatchPattern":"try {\n    Set<?> r = AmicableNumber.findAllInRange(from, to);\n} catch (IllegalArgumentException e) {\n    // bad range; ask the user for corrected bounds\n}","preventionTips":["Normalize min/max before calling so an inverted pair still works.","Validate range bounds at the UI/controller boundary.","Remember 'to' is inclusive; from==to is allowed."],"tags":["validation","maths","precondition","range","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}