Saturday 8 August 2020

NumberFormatException Explained

 NumberForamtException is thrown when you try to convert a string to numeric type, but the string is in inappropriate format to convert to the numeral.

 

Signature

public class NumberFormatException extends IllegalArgumentException{

        

}

 

Example

Integer.parseInt() method throws NumberFormatException, when you try to convert an incompatible string to an integer.

 

App.java

package com.sample.app;

public class App {

	public static void main(String args[]) {
		try {
			Integer.parseInt("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
	}
}

Output

java.lang.NumberFormatException: For input string: "abcd"
	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.sample.app.App.main(App.java:7)

Following are some of the core java methods that throw NumberFOrmatException.

 

javax.xml.bind.DatatypeConverterImpl._parseFloat(CharSequence)

javax.xml.bind.DatatypeConverterImpl._parseFloat(CharSequence)

java.math.BigDecimal.BigDecimal(char[], int, int, MathContext)

java.math.BigDecimal.parseExp(char[], int, int)

javax.xml.bind.DatatypeConverterImpl.removeOptionalPlus(CharSequence)

javax.xml.bind.DatatypeConverterImpl.removeOptionalPlus(CharSequence)

java.lang.NumberFormatException.NumberFormatException(String)

java.lang.NumberFormatException.NumberFormatException()

java.lang.NumberFormatException.NumberFormatException(String)

javax.xml.bind.DatatypeConverterImpl._parseDouble(CharSequence)

javax.xml.bind.DatatypeConverterImpl._parseDouble(CharSequence)

javax.xml.bind.DatatypeConverterImpl._parseInt(CharSequence)

javax.xml.bind.DatatypeConverterImpl._parseInt(CharSequence)

java.math.BigDecimal.adjustScale(int, long)

java.math.BigDecimal.BigDecimal(char[], int, int, MathContext)

java.math.BigDecimal.BigDecimal(double, MathContext)

java.math.BigInteger.BigInteger(byte[])

java.math.BigInteger.BigInteger(int, byte[])

java.math.BigInteger.BigInteger(int, int[])

java.math.BigInteger.BigInteger(int[])

java.math.BigInteger.BigInteger(String, int)

java.lang.Byte.decode(String)

java.lang.Long.decode(String)

java.lang.Short.decode(String)

java.lang.NumberFormatException.forInputString(String)

java.lang.Package.isCompatibleWith(String)

java.lang.Byte.parseByte(String, int)

java.math.BigInteger.parseInt(char[], int, int)

java.lang.Integer.parseInt(String, int)

java.lang.Long.parseLong(String, int)

java.lang.Short.parseShort(String, int)

java.lang.Integer.parseUnsignedInt(String, int)

java.lang.Long.parseUnsignedLong(String, int)

 

Possible Causes of NumberFormatException

a. Trying to convert non-numeric string to an integer.

 

App.java

package com.sample.app;

public class App {

	public static void main(String args[]) {
		try {
			Integer.parseInt("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
		
		System.out.println();
		
		try {
			Double.parseDouble("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
		
		System.out.println();
		
		try {
			Float.parseFloat("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
	}
}

Output

java.lang.NumberFormatException: For input string: "abcd"
	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.sample.app.App.main(App.java:7)

java.lang.NumberFormatException: For input string: "abcd"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)
	at com.sample.app.App.main(App.java:15)

java.lang.NumberFormatException: For input string: "abcd"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)
	at com.sample.app.App.main(App.java:23)

b. Initializing numeric wrapper classes with non-numeric data

Example

Integer i = new Integer("abcd");

Double d = new Double("abcd");

 

App.java

package com.sample.app;

public class App {

	public static void main(String args[]) {
		try {
			new Integer("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
		
		System.out.println();
		
		try {
			new Double("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
		
		System.out.println();
		
		try {
			new Float("abcd");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
	}
}

Output

java.lang.NumberFormatException: For input string: "abcd"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.<init>(Integer.java:867)
	at com.sample.app.App.main(App.java:7)

java.lang.NumberFormatException: For input string: "abcd"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)
	at java.lang.Double.<init>(Double.java:608)
	at com.sample.app.App.main(App.java:15)

java.lang.NumberFormatException: For input string: "abcd"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)
	at java.lang.Float.<init>(Float.java:532)
	at com.sample.app.App.main(App.java:23)

c. Passing out of range values

Example

Integer i = Integer.parseInt("1111111111111111");

 

App.java

package com.sample.app;

public class App {

	public static void main(String args[]) {
		try {
			Integer.parseInt("1111111111111111");
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
		
	}
}

Output

java.lang.NumberFormatException: For input string: "1111111111111111"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:583)
	at java.lang.Integer.parseInt(Integer.java:615)
	at com.sample.app.App.main(App.java:7)

Reference

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/NumberFormatException.html


You may like

      Interview Questions
      How to initialize string array?
      junit: How to test System.exit
      Split ArrayList to multiple List chunks
       Implement re-try-catch
      Split comma separated string to list of strings





No comments:

Post a Comment