NumberFormatException when converting whole strings in Java

Declare this variable:

private String numCarteBancaireValide=String.valueOf(((Integer.parseInt(Config.NUM_CARTE_BANCAIRE_VALIDE)   ) + (Integer.parseInt("0000000000000001"))));
Config.NUM_CARTE_BANCAIRE_VALIDE is a string.

After execution, I get this error message:

java.lang.NumberFormatException: For input string: "4111111111111111"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)

Please, can you give your advice?

+4
source share
4 answers

Use Long.parseLong()as your parameter is too large for Integer. (Maximum for an integer 2147483647)

PS: use Integer.parseInt("0000000000000001")also does not make much sense, you can replace it with 1.

+4
source

4111111111111111(which is most likely a value Config.NUM_CARTE_BANCAIRE_VALIDE) overflows the type Integer.

Better try:

//if you need a primitive
long value = Long.parseLong(Config.NUM_CARTE_BANCAIRE_VALIDE); 

or

//if you need a wrapper
Long value = Long.valueOf(Config.NUM_CARTE_BANCAIRE_VALIDE); 
+1
source

integer 2147483647. Long.parseLong 4111111111111111. - :

long l = Long.parseLong("4111111111111111");

:

As Alex said, if this number represents a credit card number, you can consider it as stringinstead of switching to long, since there are no arithmetic calculations associated with credit card numbers.

+1
source

Integer.parseIntwill try to parse an integer from String.

Yours "4111111111111111" Stringdoes not represent a valid integer type of Java, since its value will be > Integer.MAX_VALUE.

Use Long.parseLonginstead.

0
source

All Articles