🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn what is NumberFormatException in Java, its common causes, example, and how to handle NumberFormatException.
What is NumberFormatException?
NumberFormatException is a runtime exception in Java that signals an attempt to convert a string into a number, but the string does not have the appropriate format. This typically occurs when using methods like Integer.parseInt() or Double.parseDouble() on strings that don't represent valid numbers.
NumberFormatException Class Diagram
Common Causes
User Input: Users may input data that looks numeric but isn't valid. For instance, "123.45.67" might appear at a glance to be a number, but it's not a valid format.
Data Import: When importing data from external sources, there's always the risk of malformed data.
Strings with Non-Numeric Characters: Attempting to parse strings with characters that aren't valid in numbers, e.g., "12A34" or "12.34.56".
Java NumberFormatException Example
In the below example, we are trying to parse the "100ABCD" string into an integer leads to NumberFormatException:package com.javaguides.corejava;
public class NumberFormatExceptionExample {
public static void main(String[] args) {
String str1 = "100ABCD";
try {
int x = Integer.parseInt(str1); // Converting string with inappropriate format
int y = Integer.valueOf(str1);
} catch (NumberFormatException e) {
System.err.println("NumberFormatException caught!");
e.printStackTrace();
}
}
}
NumberFormatException caught!
java.lang.NumberFormatException: For input string: "100ABCD"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.javaguides.corejava.NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:9)Handling NumberFormatException
Validation: Before attempting to parse a string as a number, validate its format using regular expressions or other validation techniques.
Use Third-Party Libraries: Libraries like Apache Commons Lang provide more lenient parsing methods which can handle different number formats.
Graceful Error Messages: If parsing user input, provide a clear error message so they can correct their input.
Try-Catch: As with most exceptions, a try-catch block is your best bet for catching and handling the exception effectively.
Best Practices
- Avoid blind trust in external data.
- Always assume the possibility of incorrect formats and validate data before processing.
- Consider setting up input masks or formats in user interfaces to guide users into inputting data in the correct format.
- When parsing multiple strings in a loop, it might be beneficial to log the failures but continue processing valid entries instead of terminating the whole process.
Related Exceptions Posts
Java built-in checked exceptions:
Java built-in unchecked exceptions:
Java built-in errors:
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment