Error converting string to integer

1

I'm trying to register a client on Android's internal DB. I have a String field where I put the user's phone. This field I convert to Integer using parseInt

newCliente.setFone(Integer.parseInt(foneDesc));

If I register at most two phone numbers for the user it accepts (it was the example I used). But your I type the 9 digits of the phone that is correct it gives error:

FATAL EXCEPTION: main
  java.lang.NumberFormatException: Invalid int: "4899984829"
  at java.lang.Integer.invalidInt(Integer.java:138)
  at java.lang.Integer.parse(Integer.java:378)
  at java.lang.Integer.parseInt(Integer.java:366)
  at java.lang.Integer.parseInt(Integer.java:332)
  at artur.com.decorusfinal.cadastro.CadCliente$btGravar.onClick(CadCliente.java:68)
  at android.view.View.performClick(View.java:4204)
  at android.view.View$PerformClick.run(View.java:17355)
  at android.os.Handler.handleCallback(Handler.java:725)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)

In the error it gives says that problem is in the conversion

newCliente.setFone(Integer.parseInt(foneDesc));

If it had not worked, it would not let me nor even register the user with only 2 phone numbers.

    
asked by anonymous 08.11.2016 / 11:13

1 answer

8

It turns out the number you are trying to convert is too large to be int, convert to long ...

See the following table:

        tamanho                      mínimo                          máximo
signed    8 bit                        -128                            +127
signed   16 bit                     -32 768                         +32 767
signed   32 bit              -2 147 483 648                  +2 147 483 647
signed   64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807
unsigned  8 bit                           0                            +255
unsigned 16 bit                           0                         +65 535
unsigned 32 bit                           0                  +4 294 967 295
unsigned 64 bit                           0     +18 446 744 073 709 551 615

In Java, as per documentation

      tamanho                      mínimo                          máximo
byte:   8 bit                        -128                            +127
short: 16 bit                     -32 768                         +32 767
int:   32 bit              -2 147 483 648                  +2 147 483 647
long:  64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807

See that int is 32 bits so the maximum is 2 147 483 647 and you are trying to convert 4 899 984 829, you can convert to long (64bits) this solves your problem.

long l = Long.parseLong(foneDesc);

But how is a phone number really need to convert to number?

I hope I have helped !!

References: max value of integer

    
08.11.2016 / 11:21