0

I am learning Java / Kotlin and tried to write a simple calculator app. Everything works up until the point here I press enter to get my answer. I implemented two display functions numberDisplay. one that displays the user inputs to a textview and one that displays the answer to the same textview after the user presses enter. However, the answer the doesn't display. If i remove the second display function and stick to just one (the one that takes an array) it works. but it won't work with the second function that takes a string. I just doesn't display for some reason. I know there are probably better ways to do this, but I want to figure out why the overloading won't work.

package com.zzz.yyy.xx

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

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

        }

    var displayList = arrayListOf<String>()

    fun buttEvent(view:View){
        val butt = view as Button
        var buttID:String = "0"

        when(butt.id){
            button13.id-> buttID ="C"
            button14.id-> buttID ="D"
            button15.id-> buttID ="/"

            button16.id-> buttID ="8"
            button17.id-> buttID ="9"
            button18.id-> buttID ="X"
            button7.id-> buttID ="7"

            button19.id-> buttID ="5"
            button20.id-> buttID ="6"
            button21.id-> buttID ="-"
            button6.id-> buttID ="4"

            button22.id-> buttID ="1"
            button23.id-> buttID ="2"
            button24.id-> buttID ="3"
            button25.id-> buttID ="+"

            button26.id-> buttID ="0"
            button27.id-> buttID ="."
            button28.id-> buttID ="="
            button29.id-> buttID ="N"
            else-> buttID="null"
        }

        if(buttID == "D"){
            displayList.removeAt(displayList.count()-1)
        } else if (buttID == "C"){
            displayList.clear()
        }else if (buttID== "="){
            equals(displayList)
        }else{
            displayList.add(buttID)
        }
        numberDisplay(displayList)

    }

    fun equals(xthings:ArrayList<String>){
        var operation = ""
        var temp= ArrayList<String>()
        var indexoperation = 0
        var finalnumber= xthings.count()
        var answer:String = ""

        for (thing in xthings) {
            if (thing == "X" || thing == "-" || thing == "+" || thing == "/") {
                indexoperation = xthings.indexOf(thing)
                operation = thing
            }
        }
        temp= xthings.joinToString("").split(operation) as ArrayList<String>


        when(operation){
            "X"-> answer= (temp[0].toInt() * temp[1].toInt()).toString()
            else-> answer = "null"
        }

        numberDisplay(answer)
    }

    fun numberDisplay(arr:ArrayList<String>){
        val outputString:String = arr.joinToString("")
        showNumbers.text = outputString
    }

    fun numberDisplay(texts:String){
        showNumbers.text = texts
    }

}

I am probably missing something obvious, but I can't figure out what

edit: I made it work by adding a second onclick event. using a new one for the equal button here is the new code

class MainActivity : AppCompatActivity() {

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

        }

    var displayList = arrayListOf<String>()

    fun buttEvent(view:View){
        val butt = view as Button
        var buttID:String = "0"

        when(butt.id){
            button13.id-> buttID ="C"
            button14.id-> buttID ="D"
            button15.id-> buttID ="/"

            button16.id-> buttID ="8"
            button17.id-> buttID ="9"
            button18.id-> buttID ="X"
            button7.id-> buttID ="7"

            button19.id-> buttID ="5"
            button20.id-> buttID ="6"
            button21.id-> buttID ="-"
            button6.id-> buttID ="4"

            button22.id-> buttID ="1"
            button23.id-> buttID ="2"
            button24.id-> buttID ="3"
            button25.id-> buttID ="+"

            button26.id-> buttID ="0"
            button27.id-> buttID ="."

            button29.id-> buttID ="N"
            else-> buttID="null"
        }

        if(buttID == "D"){
            displayList.removeAt(displayList.count()-1)
        } else if (buttID == "C"){
            displayList.clear()
        }else{
            displayList.add(buttID)
        }
        numberDisplay(displayList)

    }

    fun buttEqual(view: View){

        var operation = ""
        var temp= ArrayList<String>()
        var indexoperation = 0
        var finalnumber= displayList.count()
        var answer:String = ""

        for (thing in displayList) {
            if (thing == "X" || thing == "-" || thing == "+" || thing == "/") {
                indexoperation = displayList.indexOf(thing)
                operation = thing
            }
        }
        temp= displayList.joinToString("").split(operation) as ArrayList<String>


        when(operation){
            "X"-> answer= (temp[0].toInt() * temp[1].toInt()).toString()
            else-> answer = "null"
        }

        numberDisplayx(answer)

    }


    fun numberDisplay(arr:ArrayList<String>){
        val outputString:String = arr.joinToString("")
        showNumbers.text = outputString
    }

    fun numberDisplayx(texts:String){
        showNumbers.text = texts.toString()
    }

}

I still wonder why it did not work the first time

1 Answer 1

1

Try to use function with vararg:

fun numberDisplay(vararg texts:String) {
    ...
}

Call it:

var answer:String = ""
numberDisplay(answer)

or

var displayList = arrayListOf<String>()
numberDisplay(*displayList)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.