NumberFormatException in Java: parseInt on Bad Input (AP CSA)
NumberFormatException in Java: parseInt on Non-Numeric Text (AP CSA)
A NumberFormatException happens when you try to convert a String to a number, but the String is not a valid number. The usual culprit is Integer.parseInt on text that contains letters, spaces, or symbols.
Error type: Runtime exception (compiles fine, then crashes while running).
Plain meaning: You passed a String that is not a valid number to a parsing method like Integer.parseInt.
Fastest fix: Make sure the String contains only digits (and an optional leading minus) before parsing it.
Why students see this error
Integer.parseInt reads the characters of a String and builds an int. If any character is not part of a valid integer, there is no number to build, so Java throws a NumberFormatException. Common offenders are spaces, letters, decimal points (for parseInt), and empty Strings.
- Parsing text that contains letters or symbols, like
"12a". - Leading or trailing spaces, like
" 12". - Parsing an empty String, or a decimal like
"3.5"withparseInt.
Broken code that triggers it
Throws NumberFormatExceptionString input = "12a"; // not a valid integer int n = Integer.parseInt(input); // NumberFormatException System.out.println(n);
The String "12a" starts like a number but contains the letter a. Integer.parseInt cannot build an int from it, so it throws a NumberFormatException rather than guessing or partially parsing.
Trace: watch where it breaks
Trace what parseInt sees character by character:
| Character | Valid digit? | Running parse | Result |
|---|---|---|---|
| '1' | yes | 1 | ok |
| '2' | yes | 12 | ok |
| 'a' | no | (invalid) | NumberFormatException |
parseInt does not stop at the valid prefix and return 12; any invalid character makes the entire String invalid, so the whole call throws.
The fix
Runs correctlyString input = "12"; try { int n = Integer.parseInt(input.trim()); System.out.println(n); } catch (NumberFormatException e) { System.out.println("Not a valid number"); }
Two defenses: trim() removes stray spaces that would otherwise cause a failure, and the try/catch handles input that is genuinely not a number without crashing. For AP CSA, the core point is ensuring the String is a clean integer before parsing.
How College Board tests this concept
AP Connection (Official CED)
String-to-number conversion with Integer.parseInt appears in Using Objects and Methods and in programs that process input. The exam expects you to know that parsing invalid text fails rather than returning a partial result.
Read it in the official CED: Unit 1 (CED p.23)
While exception handling with try/catch is light on the exam, the behavior of parseInt can appear: given a String with non-digit characters, the result is a thrown exception, not a truncated number.
Common MCQ traps (practice set)
Watch out
The trap is assuming parseInt reads as many digits as it can and ignores the rest. It does not. A single invalid character makes the whole String invalid and the call throws.
Question 1: Predict the result
int x = Integer.parseInt("3.5");
What is the result of running this code?
- A) x becomes 3
- B) x becomes 4
- C) x becomes 35
- D) A NumberFormatException is thrown
parseInt expects a whole integer. The decimal point in "3.5" is not a valid character for an int, so it throws. A and B wrongly assume it rounds or truncates; parsing does neither.Question 2: Which parse successfully?
Each call uses Integer.parseInt.
I. Integer.parseInt("-42")
II. Integer.parseInt(" 42")
III. Integer.parseInt("42")Which calls return an int without throwing?
- A) III only
- B) I and III
- C) I, II, and III
- D) II and III
trim() first). So I and III only.Question 3: Diagnose the cause
A program reads a String from the user and calls Integer.parseInt on it, and sometimes throws NumberFormatException.
What is the most likely fix?
- A) Validate or trim the input before parsing
- B) Change int to double in the declaration
- C) Make the method static
- D) Initialize the String to null first
Related help pages
Frequently asked questions
Does parseInt read the valid digits and ignore the rest?
No. Any invalid character makes the entire String invalid, and the call throws. It does not return a partial result.
Can parseInt handle decimals like "3.5"?
No. Integer.parseInt only accepts whole integers. A decimal point causes a NumberFormatException; use Double.parseDouble for decimals.
Why does " 42" with a space throw?
The leading space is not a digit, so the String is not a valid integer as-is. Calling trim() first removes the space and lets it parse.
Master the concepts behind the error
Work through the full AP CSA lessons, then test yourself.
AP CSA Lessons All Java ErrorsGet in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]