0

I know some countries use commas as thousands separator, but in the other countries, they use dots, for instance 1.000.000.000. So my task is to convert string number with these dots from EditTextInput to integer in Kotlin.

I use the following formula:

EditTextInteger = Integer.parseInt(edit_text_input.text.toString().replace(Regex("[.]"),""))

The code seems fine for thousands (1.000), millions (1.000.000), and up to 2 billions (2.000.000.000) but after 3 billions (3.000.000.000), the following error appears:

Process: com.example.gesit, PID: 10157
    java.lang.NumberFormatException: For input string: "3000000000"
        at java.lang.Integer.parseInt(Integer.java:747)
        at java.lang.Integer.parseInt(Integer.java:865)
        at com.example.gesit.TambahProduk$onCreate$4.onTextChanged(TambahProduk.kt:262)
        at android.widget.TextView.sendOnTextChanged(TextView.java:11888)
        at android.widget.TextView.handleTextChanged(TextView.java:12018)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:15206)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1278)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:587)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:314)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:518)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:304)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:48)
        at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:129)
        at android.widget.TextView.doKeyDown(TextView.java:9533)
        at android.widget.TextView.onKeyDown(TextView.java:9305)
        at android.view.KeyEvent.dispatch(KeyEvent.java:3664)
        at android.view.View.dispatchKeyEvent(View.java:14927)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:738)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1991)
        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:1004)
        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1952)
        at android.app.Activity.dispatchKeyEvent(Activity.java:4225)
        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:599)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3090)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:823)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:7722)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7545)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6922)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6979)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6945)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:7143)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6953)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:7200)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6926)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6979)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6945)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6953)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6926)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:10400)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:10248)
2022-08-18 19:38:22.931 10157-10157/com.example.gesit E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:10204)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:6539)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:6403)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8751)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
2022-08-18 19:38:22.977 10157-10157/com.example.gesit I/Process: Sending signal. PID: 10157 SIG: 9

Why is that? and what need to be fixed?

ADM
  • 20,406
  • 11
  • 52
  • 83
Tommy -
  • 53
  • 9
  • 1
    Use `Long` or `BigInteger` .. you are parsing a value from EditText so there has to be a Max char limit set on your `EditText` .. Otherwise anything can overflow with parsing .. – ADM Aug 18 '22 at 09:50
  • @ADM yep, using Long fix this. Thanks – Tommy - Aug 18 '22 at 10:27

0 Answers0