TheAlgorithms/Java · error · NumberFormatException
Input parameter of incorrect format:
Error message
Input parameter of incorrect format:
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/ParseInteger.java:18
package com.thealgorithms.maths;
public final class ParseInteger {
private ParseInteger() {
}
private static void checkInput(final String s) {
if (s == null) {
throw new NumberFormatException("Input parameter must not be null!");
}
if (s.isEmpty()) {
throw new NumberFormatException("Input parameter must not be empty!");
}
}
private static void checkDigitAt(final String s, final int pos) {
if (!Character.isDigit(s.charAt(pos))) {
throw new NumberFormatException("Input parameter of incorrect format: " + s);
}
}
private static int digitToInt(final char digit) {
return digit - '0';
}
/**
* Parse a string to integer
*
* @param s the string
* @return the integer value represented by the argument in decimal.
* @throws NumberFormatException if the {@code string} does not contain a
* parsable integer.
*/
public static int parseInt(final String s) {
checkInput(s);
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int v = ParseInteger.parse(raw);
// after
if (!raw.matches("^[+-]?\\d+$")) {
throw new IllegalArgumentException("not an integer: " + raw);
}
int v = ParseInteger.parse(raw); Defensive patterns
Strategy: validation
Validate before calling
if (s == null || !s.matches("^[+-]?\\d+$")) {
throw new IllegalArgumentException("not an integer: " + s);
}
int v = ParseInteger.parse(s); Try / catch
try {
int v = ParseInteger.parse(s);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("invalid integer input: " + s, e);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Input parameter must not be null!
- Input parameter must not be empty!
- Input string must be a valid integer: {s}
- Theta (angle) must be a finite number.
- Denominator cannot be 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/bb2d23208ff4fb71.
Report an issue: GitHub.