Hvaandres
Hvaandres

Reputation: 1005

I'm having an issue with the calculator that I'm building on Android

I currently have all of the buttons needed. However, the Operators button is not using the character. At the time that you click in one of the operators is not returning anything.


package com.example.mycalculator

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    var lastNumeric : Boolean = false
    var lastDot : Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }

    fun onDigit(view: View){

        tvInput.append((view as Button).text)
        lastNumeric = true
    }

    fun onClear(view: View){

        tvInput.text = ""
        lastNumeric = false
        lastDot = false


    }

    fun onDecimalPoint(view: View){
        if (lastNumeric && !lastDot){
            tvInput.append(".")
            lastNumeric = false
            lastDot = false
        }
    }


    fun onEqual(view: View){
        if(lastNumeric){
            var tvValue = tvInput.text.toString()
             var prefix = "="

            try {
                    if(tvValue.contains("-")){
                        val splitValue = tvValue.split("-")
                        var one = splitValue[0]
                        var two = splitValue[1]

                        if(!prefix.isEmpty()){
                            one = prefix + one
                        }
                    // Check if it is empty or if we have a second minus
                            tvInput.text = (one.toDouble() - two.toDouble()).toString()

                }  else if(tvValue.contains("/")){
                    val splitValue = tvValue.split("/")
                    var one = splitValue[0]
                    var two = splitValue[1]
                    // Check if it is empty or if we have a second minus
                    if(!prefix.isEmpty()){
                        one = prefix + one
                    }

                    tvInput.text = (one.toDouble() / two.toDouble()).toString()

                }else if(tvValue.contains("*")){
                    val splitValue = tvValue.split("*")
                    var one = splitValue[0]
                    var two = splitValue[1]
                    // Check if it is empty or if we have a second minus
                    if(!prefix.isEmpty()){
                        one = prefix + one
                    }
                    tvInput.text = (one.toDouble() * two.toDouble()).toString()

                }else if(tvValue.contains("+")){
                    val splitValue = tvValue.split("+")
                    var one = splitValue[0]
                    var two = splitValue[1]
                    // Check if it is empty or if we have a second minus
                    if(!prefix.isEmpty()){
                        one = prefix + one
                    }


                    tvInput.text = (one.toDouble() + two.toDouble()).toString()
                }
            }catch (e: ArithmeticException){
                e.printStackTrace()
            }
        }
    }



    fun onOperator(view: View){
        if(lastNumeric && !isOperatorAdded(tvInput.toString())){
            tvInput.append((view as Button).text)
            lastNumeric = false
        }
    }

    private fun isOperatorAdded(value: String) : Boolean {
        return if (value.startsWith("-")){
           false
        } else  value.contains("/") || value.contains("*") || value.contains("+") || value.contains("-")
    }
}

Inside of the activity_main, I have that the operator is coming through onClick element call "onOperator."

Example:


<Button
            android:id="@+id/btnAdd"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:width="0dp"
            android:text="+"
            android:onClick="onOperator"
            android:layout_margin="5dp"
            />

Does anyone know what is the best way to solve this issue?

Upvotes: 0

Views: 81

Answers (1)

snachmsm
snachmsm

Reputation: 19253

what is tvInput? you haven't declared it anywhere, but using in many places... (stripped out code?)

looks like tvInput is a TextView, you are using tvInput.text.toString() in one of your lines

so your if condition in onOperator is wrong

if(lastNumeric && !isOperatorAdded(tvInput.toString())){

you should check text placed in tvInput, not tvInput itself (as View). tvInput.toString() will return some "random" String representing this View, not content of this View. try with this line:

if(lastNumeric && !isOperatorAdded(tvInput.text.toString())){

and get familiar with logging, its crucial... you could put a Log line in onOperator and find out by yourself that these conditions/values are wrong

Upvotes: 1

Related Questions