# java.lang.NumberFormatException: For input string: "x"

**Code:** `NumberFormatException` · **Category:** Java · **Seen on:** Java, Android

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

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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/java/numberformatexception)
