{"record":{"id":"bb2d23208ff4fb71","repo":"TheAlgorithms/Java","slug":"input-parameter-of-incorrect-format","errorCode":null,"errorMessage":"Input parameter of incorrect format: ","messagePattern":"Input parameter of incorrect format: ","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ParseInteger.java","lineNumber":18,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class ParseInteger {\n    private ParseInteger() {\n    }\n\n    private static void checkInput(final String s) {\n        if (s == null) {\n            throw new NumberFormatException(\"Input parameter must not be null!\");\n        }\n        if (s.isEmpty()) {\n            throw new NumberFormatException(\"Input parameter must not be empty!\");\n        }\n    }\n\n    private static void checkDigitAt(final String s, final int pos) {\n        if (!Character.isDigit(s.charAt(pos))) {\n            throw new NumberFormatException(\"Input parameter of incorrect format: \" + s);\n        }\n    }\n\n    private static int digitToInt(final char digit) {\n        return digit - '0';\n    }\n\n    /**\n     * Parse a string to integer\n     *\n     * @param s the string\n     * @return the integer value represented by the argument in decimal.\n     * @throws NumberFormatException if the {@code string} does not contain a\n     *                               parsable integer.\n     */\n    public static int parseInt(final String s) {\n        checkInput(s);\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ParseInteger.java#L1-L36","documentation":"Thrown by ParseInteger.checkDigitAt when the character at position pos is not a digit (fails Character.isDigit). The public parse method walks each character and uses this helper to enforce that every relevant character is a decimal digit, so any non-digit (letter, symbol, whitespace, sign in the wrong place) triggers it.","triggerScenarios":"Calling parse with a string containing a non-digit character at a position checked by checkDigitAt — e.g., parse(\"12a3\"), parse(\" 123\"), parse(\"12.3\"). The exact position depends on the public parse method's loop, but any non-digit (other than an optional leading sign handled elsewhere) will trip it.","commonSituations":"User-typed input with stray characters; locale-specific formatting (thousands separators, decimal points, spaces); untrimmed leading/trailing whitespace; a value passed through String.valueOf of a non-integer object.","solutions":["Sanitize the input: trim whitespace and strip disallowed characters before parsing.","Validate with a regex such as ^[+-]?\\d+$ before calling parse.","Use Integer.parseInt if standard JDK parsing semantics (which accept a leading sign) are acceptable."],"exampleFix":"// before\nint v = ParseInteger.parse(raw);\n\n// after\nif (!raw.matches(\"^[+-]?\\\\d+$\")) {\n    throw new IllegalArgumentException(\"not an integer: \" + raw);\n}\nint v = ParseInteger.parse(raw);","handlingStrategy":"validation","validationCode":"if (s == null || !s.matches(\"^[+-]?\\\\d+$\")) {\n    throw new IllegalArgumentException(\"not an integer: \" + s);\n}\nint v = ParseInteger.parse(s);","typeGuard":null,"tryCatchPattern":"try {\n    int v = ParseInteger.parse(s);\n} catch (NumberFormatException e) {\n    throw new IllegalArgumentException(\"invalid integer input: \" + s, e);\n}","preventionTips":["Validate input shape with a regex before parsing.","Strip locale-specific formatting (separators, whitespace) upstream.","Use Integer.parseInt if standard JDK sign handling is acceptable."],"tags":["math","parsing","invalid-format","number-format"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}