isEmpty in android kotlin does not check EditText

86 Views Asked by At

My question is about Android/Kotlin. By clicking on button, there must be checking if the inputs of EditText are filled in. If not filled in, there should be a message in Toast about the error.

Here is my code:

button.setOnClickListener {
            try {

                // конвертируем данные из textedit в Int при клике на кнопку
                val minimumResult = Integer.parseInt(minimumInput.text.toString())
                val maximumResult = Integer.parseInt(maximumInput.text.toString())

                val result = (minimumResult..maximumResult).random()
                resultLabel.text = result.toString()

                val minToString = minimumInput.text.toString()
                val maxToString = maximumInput.text.toString()
                Log.d("testlogs", "$minToString и $maxToString");
                if(minToString.isEmpty() && maxToString.isEmpty()) {
                    Log.d("testlogs","EditText inputs are not filled in")
                    Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
                }
                if (minToString.isNotEmpty() && maxToString.isNotEmpty()){
                    Log.d("testlogs","Cool! That works! EditText inputs are filled in")
                    Toast.makeText(applicationContext, "Cool! That works! ", Toast.LENGTH_SHORT).show()
                }

            } catch (_: Exception) {
            }

        }

I do not know why the code "minToString.isEmpty() and maxToString.isEmpty()" does not check if the TextEdit filled in and does not display the error/or any message.

Can you tell, where is my mistake? Thank you!

Instead of isEmpty, I tried to use isBlank, match(""), minToString.length == 0, but anyway, when the inputs of TextEdit are empty (not filled in), Toast message with error does not appear.

2

There are 2 best solutions below

0
Ivo On BEST ANSWER

Parsing of the int throws an exception when it's not a number, also when it's empty. Try switch it around so it first does the empty check, so like

button.setOnClickListener {
            try {

                val minToString = minimumInput.text.toString()
                val maxToString = maximumInput.text.toString()
                Log.d("testlogs", "$minToString и $maxToString");
                if(minToString.isEmpty() && maxToString.isEmpty()) {
                    Log.d("testlogs","EditText inputs are not filled in")
                    Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
                }
                if (minToString.isNotEmpty() && maxToString.isNotEmpty()){
                    Log.d("testlogs","Cool! That works! EditText inputs are filled in")
                    Toast.makeText(applicationContext, "Cool! That works! ", Toast.LENGTH_SHORT).show()
                }

                val minimumResult = Integer.parseInt(minimumInput.text.toString())
                val maximumResult = Integer.parseInt(maximumInput.text.toString())

                val result = (minimumResult..maximumResult).random()
                resultLabel.text = result.toString()


            } catch (_: Exception) {
            }

        }
0
Yang Kang On

According to your description, your code has a NumberFormatException when you invoke Integer.parseInt(); you can try to print the Exception in the catch code block.

Here is the source code of parseInt. If the incoming String is not a number, it will throw a NumberFormatException:

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
}