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.
For input string 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 not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in Java
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026