2

I'm trying to make a calculator app, and I need to be able to get the last value after someone presses an operator (+,*,-,/) after the second time (the first value is saved with no issues since it's the only number on the screen). So if the top of the screen has something like (222 * 3333 / 12), I need to get 12. Once I learn how to do this, I can figure out how to save the previous number/sum, then do calculations on the next number (I haven't made that part yet though). I know this is a janky way of doing things, and any suggestions are appreciated. So I can grab this number if I use substringAfterLast() and insert an operator there, however, if mix things up and use multiple operators (like 222 * 3333 / 12), my y variable (see below) just shows "222 * 3333 / 12" instead of 12. How can I use multiple delimiters for substring?

Here's my code by the way (forgive me)

        multiplybutton.setOnClickListener {
            var x = numbersEntered.toString()
            var y = ""
            //creates an array that holds the operators so it can be easily filtered out
            val operators = arrayOf<Char>('*','/','+','-')
            //prevents app from crashing by saving the last value before the operator is added, allowing us to create infinite number of operations
            if (x.any(operators :: contains)){
//                x.split(operators.toString())
                
                y = x.substringAfterLast('*')  // can't use my operator character array here? why? how do I iterate through it?
                Toast.makeText(this, y.toString(), Toast.LENGTH_SHORT).show()

//                previousvalue = y.toInt()
            } else {
                previousvalue = x.toInt()
            }
            numbersEntered.append("*")
            numbersEnteredBox.setText(numbersEntered.toString())
            isMultiply = true;
            Toast.makeText(this, previousvalue.toString(), Toast.LENGTH_SHORT).show()
        }

edit: ignore italics plz, not sure what went wrong

actionjump
  • 23
  • 5
  • 1
    I guess one way to do it would be to get the last indices of `+`, `-`, `*`, and `/`. Then, you store the operator with the largest value and do your `substringAfterLast`, or (easier) you just take the substring starting one past that index. But speaking in general, this won't get you far (depending on how far you intend to go with your calculator). You'll have to work with parsers and grammars eventually. – Beko Aug 29 '20 at 07:31
  • Interesting! I didn't think about that actually. – actionjump Aug 30 '20 at 03:39

2 Answers2

3

sorry, maybe I not fully understand what you want to do but you can use split function to get last number from string like this 222 * 3333 / 12:

println("222 * 3333 / 12".split('+', '-', '*', '/').last().trim().toInt())
// prints 12
Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36
1

Use split with a regular expression

Using a regular expression gives you a great deal of control over how the string is split. In your case, anything that isn't a numeric digit is probably a delimiter. Regex has a built-in character class, denoted by \D, that will match any character that isn't a digit.

val pattern = Regex("""\D+""") // matches one or more non-digit characters
"1 * 2 / 3".split(pattern).last() // returns "3"

Equally, you could create your own character class, using [], to be more specific about which characters you want to use as delimiters:

val pattern = Regex("""\s*[-\+*]\s*""") // matches -\+*, and any spaces around them
"1 * 2 / 3".split(pattern).last() // returns "3"
Sam
  • 8,330
  • 2
  • 26
  • 51
  • Thanks! I was very hesistant about using regex because I heard about the big overhead it takes (even though that's not at all an issue with as small an app this is) and how unreadable it can be to beginners like me lol. Great answer! – actionjump Aug 30 '20 at 03:44
  • 1
    The performance of regular expressions varies depending on how complex they are. There are some good answers here, if you're interested: https://stackoverflow.com/questions/2667015/is-regex-too-slow-real-life-examples-where-simple-non-regex-alternative-is-bett – Sam Aug 30 '20 at 08:11
  • 1
    And if you haven't used regular expressions much before and just want to learn more about how to use them, a good starting point is this answer: https://stackoverflow.com/a/2759417/4618331 – Sam Aug 30 '20 at 08:14