Read Also: Exception Handling Interview Questions
The methods like Integer.parseInt(), Float.parseFloat(), Double.parseDouble() etc. throw java.lang.NumberFormatException when someone tries to convert an invalid String into number.
public class JavaHungry { public static void main(String args[]) { String s = "1"; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
1
The above code is perfect and will not throw any error or exception. The String “1” was successfully converted into Integer 1 because the String was legal to be converted into numerical data.
Now, change the above code a little bit and let see what happens:
public class JavaHungry { public static void main(String args[]) { String s = "1A"; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1A"
The above code will generate a java.lang.NumberFormatException because String “1A” was not a valid String for conversion into a primitive datatype.
What are the possible reasons for NumberFormatException?
These are the following possible reasons for NumberFormatException in Java:1. Converting Null Value to Number
In Java, String containing null value can’t be converted into a primitive data type. For example, a null String can’t be converted into an int, float, double, long, short, or byte data type.For example:
class JavaHungry { public static void main(String args[]) { String s = null; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "null"
2. Empty String to Number
In Java, Empty String can’t be converted into a primitive data type. Any String of length 0 is referred as an empty String in Java.For example:
public class JavaHungry { public static void main(String args[]) { String s = ""; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
3. Alphanumeric String
Alphanumeric String (String which is a combination of alphabetic characters and numbers) cannot be converted into primitive data types.For example:
public class JavaHungry { public static void main(String args[]) { String s = "AliveisAwesome2020"; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "AliveisAwesome2020"
4. Leading or Trailing Spaces
Strings with leading or trailing spaces are not valid Strings for conversion into primitive data types. This is one of the most possible reasons for this exception.For example:
public class JavaHungry { public static void main(String args[]) { // Trailing Space String s = "123 "; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "123 "
5. Conversion of Floating Point String into Integer
Floating-point Strings such as "1.0" are not considered as a valid String for conversion into primitive data types such as int, long, short, etc. in java.For example:
public class JavaHungry { public static void main(String args[]) { String s = "1.0"; int i = Integer.parseInt(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.0"
6. Out of Range Value
A valid String can be converted into a primitive data type if and only if its numerical value lies within the range of that data type. For example, the range of byte is -128 to 127. So, if someone tries to convert “131” or “-129” to byte, java.lang.NumberFormatException will occur at runtime.For example:
public class JavaHungry { public static void main(String args[]) { String s = "-129"; byte i = Byte.parseByte(s); System.out.println(i); } }
Output:
Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"-129" Radix:10
So, these were some common reasons for java.lang.NumberFormatException for the input string. We can handle this exception by keeping the above reasons in mind and avoiding them in our code. However, using a try-catch block is still a good choice to handle exceptions.
Reference:
NumberFormatException Oracle docs