Error in string to integer conversion

Asked

Viewed 427 times

1

I’m trying to register a client in Android internal comic book. I have a field String where I put the user’s phone. This field I do the conversion to Integer using the 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 if 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 error it gives says problem is in conversion

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

Erro na conversão

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

1 answer

8


Happens 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 as it is a phone number there is really need to convert to number?

I hope I’ve helped!!

References: max value of integer

  • I think there’s a piece missing from the table

  • @jbueno edited the answer already with the other table.

  • Now yes. Good answer +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.