Multiplying with Empty Edittexts

112 Views Asked by At

I have 7 EditTexts and I have to multiply each of them together in a formula say a∗b∗c∗d∗e∗f∗g. It is working fine but when one of these EditTexts is empty the formula yields nothing. Now the solution is I have to frame seperate cases of each one of them using if or switch statement. The issue is that there will be 2^7-1=127 such cases or combinations where one of the 7 EditTexts is empty within itself or with other edittexts. So what is the shortest way to resolve this?

2

There are 2 best solutions below

4
Cloverleaf On

You can solve your problem with a single if statement:

    if (et1.getText().toString().isEmpty() | 
        et2.getText().toString().isEmpty() |
        et3.getText().toString().isEmpty() |
        et4.getText().toString().isEmpty() |
        et5.getText().toString().isEmpty() |
        et6.getText().toString().isEmpty() |
        et7.getText().toString().isEmpty() )
    {
      // Multiplication can not be done
    } else {
      // Do the multiplication
    }
2
General Tony On

You can loop through the number EditText and convert input into numerical values and multiply them together, this will avoid huge number of conditional statements.

val editTexts = arrayOf(editText1, editText2, editText3, editText4, editText5, editText6, editText7)
    for (editText in editTexts) {
        val input = editText.text.toString().toDoubleOrNull()
        if (input != null) {
            
        }
    }