NumberFormatException Java Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see For input string
Seen on: Java Android

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

  1. Trim and validate before parsing
  2. Use Long or BigDecimal for big/decimal values
  3. Use NumberFormat for locale-formatted numbers
  4. Catch the exception and return a validation error

Detailed fix by platform

Java

  1. 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

  1. Input — What string is in quotes?
  2. Type — Does it fit int / need decimals?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

422 — HTTP 422 Unprocessable Content (HTTP)422CastError ObjectId — CastError: Cast to ObjectId failed for value "x" (type string) at path "_id" (Node.js)CastError ObjectIdDateTimeParseException — java.time.format.DateTimeParseException: Text '31/12/2024' could not be parsed at index 2 (Java)DateTimeParseExceptionDocument failed validation — MongoDB: MongoServerError: Document failed validation (code 121) (MongoDB)Document failed valid…error converting YAML to JSON — error converting YAML to JSON: yaml: line 12: did not find expected key (Kubernetes)error converting YAML…IllegalArgumentException — java.lang.IllegalArgumentException (Java)IllegalArgumentExcept…Input string was not in a correct format — System.FormatException: Input string was not in a correct format (.NET)Input string was not …InputMismatchException — java.util.InputMismatchException (Scanner) (Java)InputMismatchExceptionNumberFor…

Explore the map of this error Full error map →

🧠 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.