0

I am collecting values from two edittext fields on button click. I've implemented handling for all of the necessary conditions (negative numbers, too big value, zero), but the case with empty edittext keeps crashing, despite handling that case also.

fun buildAlertDialog(message : String = "Bad input value")
        {
            val alertDialogBuilder = AlertDialog.Builder(this@MainActivity)

            alertDialogBuilder.setTitle("Error")
            alertDialogBuilder.setMessage("$message")

            alertDialogBuilder.setNeutralButton("Close"){_,_ ->}

            val alertDialog : AlertDialog = alertDialogBuilder.create()
            alertDialog.show()
        }

btnCalculate.setOnClickListener{
            var heightInput = editTxt_EnterHeight.text.toString().toDouble()
            var weightInput = editTxt_EnterWeight.text.toString().toInt()

            if(editTxt_EnterHeight.toString().trim().isNotEmpty())
            {
                Toast.makeText(this, "Height is not empty", Toast.LENGTH_SHORT).show()
                if(heightInput > 2.5)
                {
                    buildAlertDialog("Bad input value - too tall")
                }
                else if(heightInput == 0.0)
                {
                    buildAlertDialog("Bad input value - height value can't be zero")
                }
                else if(heightInput < 0)
                {
                    buildAlertDialog("Bad input value - height can't be a negative number")
                }
            }
            else if(editTxt_EnterHeight.text.toString().trim().length == 0)
            {
                buildAlertDialog("Plese enter height value")
            }

            if(editTxt_EnterWeight.toString().trim().isNotEmpty())
            {
                Toast.makeText(this, "Weight is not empty", Toast.LENGTH_SHORT).show()
                if(weightInput > 350)
                {
                    buildAlertDialog("Bad input value - too big weight value")
                }
                else if(weightInput == 0)
                {
                    buildAlertDialog("Bad input value - weight can't be zero")
                }
                else if(weightInput < 0)
                {
                    buildAlertDialog("Bad input value - weight can't be a negative number")
                }
            }
            else if(editTxt_EnterWeight.text.toString().trim().length == 0)
            {
                buildAlertDialog("Please enter weight value")
            }
        }

Height edittext inputType is set as numberDecimal, while weight edittext is set as number.

I've tried implementing multiple solutions I found on stackoverflow and other sites, but all of them had the same result. (Check if EditText is empty kotlin android, https://www.tutorialspoint.com/how-to-check-if-android-edittext-is-empty, https://inducesmile.com/kotlin-source-code/how-to-check-if-edittext-is-empty-in-kotlin/)

Apollo
  • 118
  • 1
  • 9

1 Answers1

0

Bro u have to check the text before u cast it to double, not after that. Move it down:

 var heightInput = editTxt_EnterHeight.text.toString().toDouble()
 var weightInput = editTxt_EnterWeight.text.toString().toInt()
Thành Hà Văn
  • 481
  • 2
  • 9
  • I've removed both of the variables and have used `editTxt_EnterHeight.text.toString().toDouble()` to get the values needed, but the same issue happens. – Apollo Mar 19 '20 at 18:16
  • Managed to solve it. Instead of using `isNotEmpty` in if conditions to check if edittext is not empty, i used the following `editTxt_EnterWeight.text.toString().length > 0` to check the same condition. `if(editTxt_EnterWeight.text.toString().length > 0) { ... } else {...} ` I'm a bit flambergasted as to why this works, whereas the `isNotEmpty` option does not work. Ironically, `isNotEmpty` is recommended by Android Studio/Kotlin. – Apollo Mar 19 '20 at 18:33