0

I am getting this exception in certain devices mostly API29. So am assigning double rounded values to Toggle buttons. When the user clicks on them it adds that value to the current total, then convert that total into a String in order to be displayed in a TextView.

I have tried adding the exception but it fails to display the double price assigned to the toggle button.

Also, I viewed some other threades in here which wehere not helpful.

The code is below; Any guidance on this?

private void setListeners() {


        //boolean chemoIsChecked = false;

        chemoBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                    if (isChecked) {
                        try{

                            Double chemoPrice = Double.valueOf(chemoButtonPrice);
                            d = chemoPrice + d;
                            String totalPrice = String.valueOf(String.format(Locale.GERMAN, "%.2f", d));
                            premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);

                        }catch (NumberFormatException e){

                        }

                    } else {
                        try{

                        }catch (NumberFormatException e){
                            Double chemoPrice = Double.valueOf(chemoButtonPrice);
                            d = d - chemoPrice;
                            String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                            premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                        }

                    }

            }
        });

        cremBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Double cremPrice = Double.valueOf(cremationButtonPrice);
                    d = cremPrice + d;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                } else {
                    Double cremPrice = Double.valueOf(cremationButtonPrice);
                    d = d - cremPrice;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                }
            }

        });

        travenBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Double travelPrice = Double.valueOf(travelButtonPrice);
                    d = travelPrice + d;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                } else {
                    Double travelPrice = Double.valueOf(travelButtonPrice);
                    d = d - travelPrice;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                }
            }

        });

        btnContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createAdditionalPackages();


}
Fatal Exception: java.lang.NumberFormatException: For input string: "3,78"
       at sun.misc.FloatingDecimal.readJavaFormatString + 2043(FloatingDecimal.java:2043)
       at sun.misc.FloatingDecimal.parseDouble + 110(FloatingDecimal.java:110)
       at java.lang.Double.parseDouble + 538(Double.java:538)
       at java.lang.Double.valueOf + 502(Double.java:502)
       at nl.risk.www.smart2coverapp.activity.PetplanAdditionalPlansActivity$2.onCheckedChanged + 165(PetplanAdditionalPlansActivity.java:165)
       at android.widget.CompoundButton.setChecked + 182(CompoundButton.java:182)
       at android.widget.ToggleButton.setChecked + 71(ToggleButton.java:71)
       at android.widget.CompoundButton.toggle + 136(CompoundButton.java:136)
       at android.widget.CompoundButton.performClick + 141(CompoundButton.java:141)
       at android.view.View.performClickInternal + 7312(View.java:7312)
       at android.view.View.access$3200 + 846(View.java:846)
       at android.view.View$PerformClick.run + 27794(View.java:27794)
       at android.os.Handler.handleCallback + 873(Handler.java:873)
       at android.os.Handler.dispatchMessage + 99(Handler.java:99)
       at android.os.Looper.loop + 214(Looper.java:214)
       at android.app.ActivityThread.main + 7099(ActivityThread.java:7099)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AndroidKrayze
  • 349
  • 2
  • 5
  • 21

2 Answers2

0

Instead of doing:

Double travelPrice = Double.valueOf(travelButtonPrice);

convert first the commas to dots with replace:

Double travelPrice = Double.valueOf(travelButtonPrice.replace(",", "."));
Buddy Christ
  • 1,364
  • 8
  • 22
0

Please remove that comma(,) using replace method of String class like below

String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d)).replace(',', '').trim()
Jaimil Patel
  • 1,301
  • 6
  • 13