Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer.parseInt and NumberFormatException on Android

Tags:

java

android

I ran the following code in my Android app

Integer.parseInt("+91"); 

In Android 5.0 (Lollipop), it did not throw any exception as +91 is an integer. But in Android 4.4.x (KitKat) and lower versions it throws:

NumberFormatException : Invalid Int : "+91"

How is the version of Android causing this difference?

like image 789
Ashwin Avatar asked May 06 '15 08:05

Ashwin


People also ask

What is NumberFormatException in parseInt?

The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).

What is parseInt in Android?

parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

Can parseInt throw error?

parseInt method as a parameter. The method throws an error if the string cannot be parsed into an integer. Note, that the code within the catch block is executed only if an exception is thrown.

What is the numberformatexception in Java?

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java. It is a subclass of IllegalArgumentException class.

How to parse an integer in Java?

Java Integer parseInt () Method. 1 1. Java Integer parseInt (String s) Method. This method parses the String argument as a signed decimal integer object. The characters in the string ... 2 2. Java Integer parseInt (String s, int radix) Method. 3 3. Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)

What is the difference between integer parseInt and integer valueOf?

Differences between Integer.parseInt() and Integer.valueOf() Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.valueOf() whereas only a String can be passed as parameter to Integer.parseInt().

Why is parseInt not available in Android SDK 19?

KitKat did introduce some java 7 features into Android sdk 19, but not the new parseInt. Lower versions use an earlier implementation of parseInt (Java 6's version) so they will obviously fail as well. The difference between parseInt implementations : Java 6 parseInt documentation vs Java 7 parseInt documentation


1 Answers

Support for explicit + was added in this commit:

Support explicit + in Byte, Short, Integer, Long.  Bug: 5239391 Change-Id: I2b25228815d70d570d537db0ed9b5b759f25b5a3 

which has been included starting with android-5.0.0_r1. If you have fetched the Git repository, you can verify with:

git tag --contains 6b40837ee3a023bba698c38fd6d6e46ae0065a55 

which gives you

android-5.0.0_r1 android-5.0.0_r2 android-5.0.0_r3 ... 

Even though documentation can give insights into why the change was made (to achieve Java 7 behavior as other answers point out), analyzing the history of the source code gives the most accurate answer to when the behavior changed, since documentation does not necessarily match implementation.

like image 130
Martin Nordholts Avatar answered Sep 18 '22 16:09

Martin Nordholts