#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2

    Arrow 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:
    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.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Look at the Long class. It also has a parse method. Or the BigInteger class.
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2
    Great thanks, Long is working just fine

IMN logo majestic logo threadwatch logo seochat tools logo