this is my html
code
<div class="form-group col-md-6">
<input type="text" class="form-control" name="phonenumber" placeholder="Enter Phone Number">
</div>
this is my controller
int phonenumber=Integer.parseInt(request.getParameter("phonenumber").trim());
I am gettting error of NumberFormatException for input string '9999999999'
How to solve it.
Even though it is a number why cannot I parse it?
The
Exception
because you are trying to convert '9999999999' into an Integer, and the max range of typeint
is2147483647
.So try
Long.parseLong("9999999999")
instead if you are insisting on converting phone number fromString
into numbers. Storing and manipulating phone numbers asint
orlong
will result in some inconsistencies in the future.If you are doing that to check whether all the input characters are digits or not, you can use other ways such as using Regular Expressions. This way is very helpful since you can check formats, separator, etc. See this sample from MKyoung site:
And another simple way is to have a method which checks all the digits of a phone number are really digits:
Good Luck.
9999999999
is outside the valid range of values for theint
data type (-231 to 231-1, inclusive), as specified by theInteger.MIN_VALUE
andInteger.MAX_VALUE
constants.You cannot represent a full phone number in an
int
, you would have to omit the prefix and area code (0000000 - 9999999). Otherwise, use along
instead (-263 to 263-1, inclusive),Long.parseLong()
will happily handle9999999999
.Check parseInt() method in Oracles doc parseInt
It clearly says
An exception of type NumberFormatException is thrown if any of the following situations occurs:
Examples:
The range of an
int
in Java is-2,147,483,648
to2,147,483,647
.9,999,999,999
is out of range and that is what is causing the exception.