7

I am making a web-based application and i have text-fields where the values are stored as Strings. The problem is that some of the text-fields are to be parsed into ints and you can store much bigger numbers in Strings than you can in an int. My question is, what is the best way to make sure that the String number can be parsed into an int without erroring out.

user1423793
  • 279
  • 2
  • 6
  • 16

7 Answers7

11

You can use a try/catch structure for that.

try {
    Integer.parseInt(yourString);
    //new BigInteger(yourString);
    //Use the above if parsing amounts beyond the range of an Integer.
} catch (NumberFormatException e) {
    /* Fix the problem */
}
Michael
  • 1,239
  • 9
  • 14
5

The Integer.parseInt method checks the range as is explicited by the javadoc :

An exception of type NumberFormatException is thrown if any of the following situations occurs:
The first argument is null or is a string of length zero.
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
The value represented by the string is not a value of type int.
Examples:
 parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787

So the correct way is to try parsing the string :

try {
    Integer.parseInt(str);
} catch (NumberFormatException e) {
    // not an int
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
3

Parse the string to a BigInteger instead of a regular Integer. This can hold much higher values.

BigInteger theInteger = new BigInteger(stringToBeParsed);
John Snow
  • 5,214
  • 4
  • 37
  • 44
0

You could run a check in your code:

  • Convert the String into a long.
  • Compare the long against the max value for an integer (a constant within the Integer class.)
  • If the long is greater than it, you know that it cannot be parsed into an int without overflow.
  • If it is less than or equal to it, convert your long into an int.
BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
0

Always parse the string in try catch block so if any exception or error occurred you know that there is some error in String to int parsing.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

You could use Apache Commons Lang.

import org.apache.commons.lang.math.NumberUtils;

NumberUtils.toInt( "", 10 );   // returns 10
NumberUtils.toInt( null, 10 ); // returns 10
NumberUtils.toInt( "1", 0 );  // returns 1

The second number is the default if the String is not a numeric value. The first parameter is the String you are trying to convert.

For large numbers I would do the following

BigInteger val = null;
try {
  val = new BigInteger( "1" );
} catch ( NumberFormatException e ) {
  val = BigInteger.ZERO;
}
mrswadge
  • 1,659
  • 1
  • 20
  • 43
0

what about this ?

BigInteger bigInt = BigInteger(numberAsString);
boolean fitsInInt = ( bigInt.compareTo( BigInteger.valueOf(bigInt.intValue()) ) == 0;
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53