Help with code to deal with empty Edit boxes when doing calculation

Android Studio, using Kotlin (MainActivity.kt)

    /* I am trying to add (base price + taxes, if any + shipping costs, if any + other fees, if any )to calculate the unit price,
            when I enter all 4 values then the app works, and if I leave even one blank (as it may not apply  when applied on day to day basis, and the user may leave these blank), then the app crashes, so how could
            I modify this code. Would appreciate help
             */
            if (taxes == null && shipping == null && otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() / units.toString().toDouble())).toString()
            }else if (shipping == null && otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble())/ units.toString().toDouble()).toString()
            }else if (otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble() + shipping.toString()
                        .toDouble())/ units.toString().toDouble()).toString()
            } else if (shipping == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble()  + otherFees.toString().toDouble())/ units.toString().toDouble()).toString()
            }
            else{
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble() + shipping.toString()
                        .toDouble() + otherFees.toString().toDouble())/ units.toString().toDouble()).toString()
            }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks. That was the first time, as I am novice in coding, so pardon me for initial errors.

code is as follows

    btnCalculate.setOnClickListener{

            /* I am trying to add base price + taxes, if any + shipping costs if any + other fees, if any to calculate unit price,
            when I enter all 4 values then the app works, and I leave even one blank (as it may not apply, then the app crashes, so how could
            I modify this code. Would appreciate help
             */
           /* if (taxes == null && shipping == null && otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() / units.toString().toDouble())).toString()
            }else if (shipping == null && otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble())/ units.toString().toDouble()).toString()
            }else if (otherFees == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble() + shipping.toString()
                        .toDouble())/ units.toString().toDouble()).toString()
            } else if (shipping == null){
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble()  + otherFees.toString().toDouble())/ units.toString().toDouble()).toString()
            }
            else{
                tvUnitPrice =
                    ((basePrice.toString().toDouble() + taxes.toString().toDouble() + shipping.toString()
                        .toDouble() + otherFees.toString().toDouble())/ units.toString().toDouble()).toString()
            }
Hope this formatting using backticks would be better and easy to red.

No need to worry. Figured out after multiple tries.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.