java.lang.NumberFormatException: For input string: "x"
A string couldn’t be parsed as a number — it’s empty, has spaces/commas, is too large, or isn’t numeric.
Meaning
Integer.parseInt, Long.parseLong and Double.parseDouble are strict. The message shows the exact input in quotes — look for empty strings, whitespace, thousands separators, decimals passed to parseInt, or values beyond Integer.MAX_VALUE.
Common causes
- Empty or null-ish string (
"","null") - Whitespace or newline (
" 42\n") - Thousands separators/currency (
"1,200","₹500") - Decimal to parseInt (
"3.5") - Overflow (
"3000000000"for int)
⚡ Quick fix
- Trim and validate before parsing
- Use Long or BigDecimal for big/decimal values
- Use NumberFormat for locale-formatted numbers
- Catch the exception and return a validation error
Detailed fix by platform
Java
- Defensive parsing:java
static Optional<Integer> parseIntSafe(String s) { if (s == null || s.isBlank()) return Optional.empty(); try { return Optional.of(Integer.parseInt(s.trim())); } catch (NumberFormatException e) { return Optional.empty(); } }
How to diagnose
- Input — What string is in quotes?
- Type — Does it fit int / need decimals?
- Source — Form, CSV, env var?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026