February 7th, 2013, 04:31 AM
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...
int argasint = Integer.parseInt(args);
if( argasint <= 2147483647 ) // do something
...won't work because if args contains a number bigger than 2147483647 (i.e. 2200000000), then Integer.parseInt will produce an error.
February 7th, 2013, 05:50 AM
Look at the Long class. It also has a parse method. Or the BigInteger class.
February 7th, 2013, 06:25 AM
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.
Originally Posted by EffX
February 7th, 2013, 03:12 PM
Great thanks, Long is working just fine