Java Integer.parseInt() Not Working for Large Numbers
Solution 1
try using Long.parseLong(IPNumbers)
Solution 2
Why not use a regular expression, or break it down into its components by using split(".")
?
There are other limitations besides needing to consist solely of digits. For example:
666.666.666.666
This will parse just fine, but is an... unlikely IP.
Breaking it up into its parts lets you determine (a) that it has four parts, and (b) that each part actually makes sense in the context of an IP address.
You could also use an InetAddress
implementation, or InetAddressValidator
from Apache Commons.
Solution 3
For very large integers you may want to use the BigInteger class (or BigDecimal), as the values may exceed the limits of Integer.
Integer Limits:
Minimum : -2147483648
Maximum : 2147483647
Using BigInteger:
string s = "239255255255";
BigInteger yourNumber = new BigInteger(s);
Solution 4
Range of an Integer
is -2,147,483,648 to 2,147,483,647, the number you have above mentioned is not included in these range, so use long
, the range of long
primitive type is in between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
So for your case its better to use Long.parseLong()
function to convert such large number to long
type.
Solution 5
239255255255 is too big to be held by an Integer. Try using a BigInteger or a BigDecimal.
Oliver Spryn
My first programming experience was a trial-by-fire adventure. It wasn't a "Hello World" application but a full-fledged, web-based learning management system. This exposure galvanized my career choice and prepared me to face a myriad of challenges head-on. My unconventional start continues to yield tangible successes in nearly every discipline I touch, whether on the technical, operational, or business level. Since then, I've been on a mission to make technology work seamlessly and feel invisible. I have delivered this continued success with a daily, sleeves-rolled-up approach. Whenever my superiors need their most complex projects to be built and flawlessly delivered, they ask my team. I keep a good rapport with my colleges and managers so that working as a team is flawless. Their confidence in me has enabled me to be at the forefront of the engineering and design efforts necessary to bring applications from 0 to over 600k users. Building projects of this quality becomes a craft. The concepts I've worked to develop, discover, and distill have worked so well that they have been featured on the droidcon blog, home of Europe's foremost Android conference. Whether my work is published on a prominent blog or neatly packed inside of some back-end service, I ensure that I conduct each of these projects with my full measure of integrity. This trait is essential in delivering healthy projects on time, and it sets the good projects apart from the great ones. Prominent Skills: Android, Kotlin, Gradle, Azure Media Services, Azure Functions, Cloudflare Workers, Adobe Premiere Pro, Adobe Illustrator, All Forms of Communication, Videography, Listening, Critical Thinking
Updated on July 03, 2022Comments
-
Oliver Spryn almost 2 years
I have the following simple piece of code which is intended to detect that a given IPv4 address indeed only has numeric values (that is after the dots have been stripped):
import edu.gcc.processing.exceptions.net.IPAddressNumericException; //Get the IP address String address = "239.255.255.255"; //Check to see if this is a number try { String IPNumbers = address.replace(".", ""); Integer.parseInt(IPNumbers); } catch (NumberFormatException e) { System.out.print(e.getMessage()); }
For some reason, the
NumberFormatException
is fired, and I get this error:For input string: "239255255255"
Could someone please help me understand this? The
parseInt()
method works on smaller numbers, such as127001
.Thank you for your time.
-
Rion Williams over 12 yearsAlso - a very unfortunate one :)
-
Oliver Spryn over 12 yearshmm... What method can I use to convert the string to an integer?
-
Rion Williams over 12 yearsThere is also a BigDecimal class if you need one, I'll include the necessary documentation in an edit.
-
Steve Kuo over 12 years239255255255 is not an integer. See docs.oracle.com/javase/6/docs/api/java/lang/…
-
Oliver Spryn over 12 yearsThat's exactly what I needed. Thanks!
-
Rion Williams over 12 years@spryno724, An integer will be unable to hold that large of a value, you will have to use BigInteger to store it.
-
Robin over 12 years+1 for actually suggesting that the approach of just checking whether it is a number is insufficient, and that switching to a long will not solve this