Parsing a string that cannot be converted to the expected numeric type.
A NumberFormatException is a subclass of `IllegalArgumentException` and is thrown when you try to convert a string to a numeric type (using `Integer.parseInt()`, `Double.parseDouble()`, etc.) but the string's content is not a valid representation of that number.
Common causes: the string contains non-numeric characters (spaces, letters, symbols), the string is empty or null, the number exceeds the type's range (overflow), the string uses a format the parser doesn't expect (e.g., a comma as a decimal separator), or extra whitespace in the string.
1public class Main {2 public static void main(String[] args) {3 String ageInput = " 25 "; // Extra whitespace4 String priceInput = "$19.99"; // Dollar sign5 String emptyInput = ""; // Empty string6 7 int age = Integer.parseInt(ageInput); // Throws8 double price = Double.parseDouble(priceInput); // Throws9 int num = Integer.parseInt(emptyInput); // Throws10 }11}1public class Main {2 public static void main(String[] args) {3 // Fix 1: Trim whitespace before parsing4 String ageInput = " 25 ";5 int age = Integer.parseInt(ageInput.trim());6 System.out.println(age); // 257 8 // Fix 2: Strip non-numeric characters before parsing9 String priceInput = "$19.99";10 String cleanPrice = priceInput.replaceAll("[^0-9.]", "");11 double price = Double.parseDouble(cleanPrice);12 System.out.println(price); // 19.9913 14 // Fix 3: Use try/catch to handle invalid input gracefully15 String userInput = "abc";16 try {17 int value = Integer.parseInt(userInput.trim());18 System.out.println("Parsed: " + value);19 } catch (NumberFormatException e) {20 System.err.println("Invalid number: '" + userInput + "'");21 }22 }23}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
`Integer.parseInt(" 25 ")` throws because the string has leading/trailing spaces — unlike some languages, Java's `parseInt` does not trim whitespace. `Double.parseDouble("$19.99")` fails because `$` is not a valid numeric character. An empty string also triggers a NumberFormatException.