NumberFormatException in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

NumberFormatException is an unchecked exception, a subclass of IllegalArgumentException. In this class, a serializable interface is implemented. It occurs while converting a String with the improper format to other numeric forms like Integer, or float.

Introduction to NumberFormatException in Java

It is thrown while converting a string of improper format to a numeric value like an integer or float. An example of improper format can be a string that has a boolean value. If this string is converted to a numeric value, then an exception is thrown. For instance, parsing a string that is null to an integer. It is an unchecked exception, also known as a run time exception. The try-catch block is used for handling this exception. When operating with the string, a parseInt() method is used for converting a string to an integer type. parseInt() method can be used in two ways as given below:

Both methods are used for converting a string to an integer. The only difference between these two syntaxes is the parameter radix. When radix=10, the first method is considered equivalent to that of the second method.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Constructors of NumberFormatException

ConstructorDescription
public NumberFormatException()default constructor, an exception, NumberFormatException with no description of it.
public NumberFormatException(String msg)Constructs an exception, NumberFormatException with a detailed description of it, parameterized, with an error message .

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

What Causes NumberFormatException?

There are various problems related to formatting string which is improper. Here are a few important reasons for NumberFormatException:

  • Null input string: Integer.parseInt("null");
  • Input string which is empty: Float.parseFloat(“”);
  • Leading or trailing white spaces in input string: Integer abc=new Integer(“ 123 “);
  • Extra symbol in input string: Float.parseFloat(4,123);
  • Input string with data which is non-numeric: Double.parseDouble(“FifityFive”);
  • Alphanumeric input string: Integer.valueOf(“33.three”);
  • Input string with exceeded range of datatype: Integer.parseInt(“1826589745243”);
  • Type mismatch between the value of input string and its method: Integer.parseInt("12.33");

Example of NumberFormatException

Program to illustrate NumberFormatException in java. In this example, we will try to understand how and why NumberFormartException occurs.

Output:

Explanation: As per code, as soon as the user enters a number other than Integer like double or float then the NumberFormatException occurs.

Flow of the given code:

  • Input is taken from user
  • The input is parsed using parseInt
  • If there is an error while parsing
  • NumberFormatException is thrown
  • Else Output is printed

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How to Handle NumberFormatException?

The NumberFormatException in java can be handled in two ways:

  1. Using the block try-catch : This involves surrounding the code that might cause the exception with a try block and handling the exception in a catch block. For example:

Output:

  1. Using throws keyword : This involves declaring that a method might throw a specific exception and leaving it to the calling method to handle it. For example, a method that parses a string to an integer might declare that it throws a NumberFormatException if the string is not a valid integer. The calling method would then have to handle this exception using a try-catch block or declare that it throws the exception using the throws keyword.

For example:

Output:

Conclusion

  • NumberFormatException is thrown while converting a string of improper format to a numeric value like an integer or float. An example of improper format can be a string that has a boolean value. If this string is converted to a numeric value, then an exception is thrown.
  • It is an unchecked exception, also known as a run time exception. The try-catch block is used for handling this exception.
  • Constructors of NumberFormatException:
    • public NumberFormatException()
    • public NumberFormatException(String msg)
  • The NumberFormatException in java can be handled in two ways:
    • Using the block try-catch in the program or around the code that can cause this exception.
    • Using throws keyword