1 year ago

#367138

test-img

Camp Nerd

If and else statement in Kotlin

Ok I have an issue with the logic of the if and else statement I am not understanding why and how the logic doesn't work. I have used the statement in other programming but this is what I am wanting to do.

I am using a button with my sqrt function. In the button I am trying to use an escape route. When I press the button it gives the square root. However when I press the button again it crashes.

So for a temporary fix and learning purpose I have done this:

 binding.btnSqrt.setOnClickListener{
    val df = DecimalFormat("#.####")
    if(!tv_equation.text.isNullOrEmpty())
    {
        val number = tv_equation.text.toString().toDouble()
        binding.tvResult.text = df.format(sqrt(number))
    else
      { 
       binding.tvResult.text = ""
      binding.tvEquation.append("")
}

However it does not imply the if else statement. I am looking at this how the statement works in WPF and UWP.

When I press the button again after that it just doubles the answer. For example in tvResult if I have 4 then when the button is pressed again it should clear but what it does is double and adds what ever is there so it shows 44 instead of clearing the textview and edittext.

Am I using the if and else statement wrong in kotlin. Or is it just the way the button is originally coded?

kotlin

button

textview

android-edittext

0 Answers

Your Answer

Accepted video resources