The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Check string is less than 2^31-1
Discuss Check string is less than 2^31-1 in the Java Help forum on Dev Shed. Check string is less than 2^31-1 Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 7th, 2013, 04:31 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
|
|
Check string is less than 2^31-1
I am taking input from the command line and part of my program is parsing the strings into integers.
I want the program to 'do something' when the number is less than or equal to the largest int (2^31-1). How do I check this is the case since I cannot represent larger numbers as integers?
In other words...
Java Code:
Original
- Java Code |
|
|
|
int argasint = Integer. parseInt(args [0]); if( argasint <= 2147483647 ) // do something
...won't work because if args[0] contains a number bigger than 2147483647 (i.e. 2200000000), then Integer.parseInt will produce an error.
|

February 7th, 2013, 05:50 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Look at the Long class. It also has a parse method. Or the BigInteger class.
|

February 7th, 2013, 06:25 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
Quote: | Originally Posted by EffX
...won't work because if args[0] contains a number bigger than 2147483647 (i.e. 2200000000), then Integer.parseInt will produce an error. |
It's an Exception rather than an error (specifically a NumberFormatException). Depending on what you want to do if an invalid input is given, you could catch the exception. Of course this will not differentiate between an input that represents a numeric value that's too high, or simply non-numeric input.
|

February 7th, 2013, 03:12 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
|
|
|
Great thanks, Long is working just fine
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|