Welcome back.
In this video, we are going to implement our unequal functionality, at least to a certain degree,
because it's a pretty long code.
We are going to require here and I want to start with a minus.
So let's say I want to subtract something and press the equals sign.
Then I want to have the result on the screen instead of having.
Just a text here.
All right, so in order to do so, we need a new function and I'm going to call this function on equal,
on equal, and it's going to require a view, as we had before.
So now I can already implement this for our equal button.
So here I'm going to add a unclick event to it and I'm going to call this one on equal or Enteron equal.
So it's going to call this function once we click on it.
All right.
So now in here, the first thing I need to check if the value that was entered is numeric, because
if we look at it, if the value is not numeric, so clear again.
So let's say we have this, then there is no calculation.
It has to finish with a numeric value.
Otherwise we don't want to calculate anything.
So let's check if last numeric.
So only if that's the case, we run any code now in here, we need to know the value of our TextField.
And in order to get it, I'm going to store it in a variable which I'm going to call TV value, which
stands for text view value.
All right.
And it's going to get the TV input dot, text, dot to string.
So what that does is it just gets whatever we have in this TV input and transfers it into a string.
So TV value will be a string.
And now we can go ahead and try something, because potentially that could something go wrong, right?
So I'm just going to say try and catch.
So implement to try and catch functionality.
The only thing that I will pass here is an arithmetic exception.
So if we ever get one of those, then I want to use it or display.
The era of print stack trace, so this is just try and catch so that our program doesn't crash if any
of the operations that we're trying to do goes wrong.
All right, so what are we going to try?
Well, let's start with trying if it's a minus, so.
If our TV value contains.
A minus.
Then I want to.
Calculate.
The subtraction, right, so I want to finally do the results or print the result onto our TV input
TV and put thought text should be the result of whatever the calculation is.
And this one is TV input.
Now, before we can implement that, we need to go ahead and split up our text here, because for so
far, we have, for example, let's say we have ninety nine minus one.
All right.
So we have 99 problems, but now we have one less.
So I would like to do the calculation once I press this button here at bottom.
And it will need to understand what's on the left hand side of the minus and what's on the right hand
side of the minus.
So what we need to do is we need to split a string and that is something that we haven't done yet.
So I want to show you how to split strings.
So I'm going to call this variable split value and it's just going to take the TV value, which is our
TextView value, which is pretty much the text that we have in our text.
You.
And it's going to split.
Now, there are a bunch of things that we can do with split, if you want to know more about it.
Check out the documentation.
So there are different versions that you can use here.
The one that I want to use is where just say at which point it should split it.
And in my case, it should split it at the minus.
OK, so I wanted to split the text at the minus into two values.
Well, in how ever many values there are which are separated by a minus.
But in our case it's only going to be one because that's how we developed our program.
Right.
So it's only going to be one minus, which means we are only going to have two values left and right
so we can use those.
And I'm going to say var one is going to be the split value add position zero.
And then Vade two, which is going to be the split value at position one.
So what does that mean?
So, for example, let's say we have.
Fifty five or ninety nine minus one then this year.
We'll be ninety nine and this year will be one.
Because they are text that we had here, this one was split at the minus itself, so this is whatever
is the first entry in this split value, which is an array, and this is the second entry.
OK.
So this was just one example.
And now what we can do, we can go ahead and actually.
Add the result to our TV input text, so what I'm going to do is I'm just going to say one dot, two
double.
So I'm going to make a double from it, plus two thought to double.
And actually it's not a plus, but it's minus right to.
But to double, of course, this should be a minus.
And of course, I need to make a string from it, because we try to put it into a text here, OK, into
a TV input of which we want to change the text field.
OK, so now let's try this.
Let's see if our code already works, because so far it's very generic what we have here, but it should
already work, at least for the minus button.
So ninety nine minus one equals and we are ninety eight dot zero.
Perfect.
So now let's do 66 minus ninety nine and we are minus thirty three.
Now the problem is if I add another minus here and I do this calculation, a program closes.
So we have an error here.
We have a crash.
So how do we fix that.
That is a tricky one.
And why does it even appear so?
Where does it come from?
It comes from exactly what we have here, because we have a minus now, then we split and then we have
another minus.
Let's say we have this minus thirty three minus another value.
So now it's going to split at the minus is right.
So it's going to split here and it's going to split here.
So this one will be whatever is before the minus.
The first one and two will be this 33.
Now we have another minus.
So this one will be three.
So if we had VARE three, then it would be split value at the position, too.
OK, but that's not the case because we don't want to do it like that, OK?
And that's also where the problem comes from, by the way, that we try to use empty here as our one.
So one is at this point empty, which is null, and we try to subtract 33 from null.
That's what we do here.
And that, of course, doesn't work, so you cannot have null values, so we get no pointer exception
and it just breaks our whole application.
So what we need to do is we need to check if there is a prefix to our text.
And that's how I'm going to do it here.
I'm going to create the new variable called prefix.
And it's going to be an empty string at that time, and then I'm going to double check before I do this
calculation that we have here.
I'm going to double check if TV value, Don, starts with a minus.
So if the prefix is a minus.
Then I want to set our prefix as this minus and then I want to set TV value.
As being TV value, not a substring at the position one.
So what does this substring do?
Well, if you want to know more about it, keep the control.
Keep rest.
Go to substring return to substring of this string that starts at the specified START index and continues
to the end of the string.
OK, so the start indexes, whatever we pass to the substring method, and that's what we do here,
we add to value one.
So let's say we had a value of minus 250.
This minus is the strings start index zero, and this, too, is the start index one.
So what we say with this line is just this regard, this minus four now.
That's it, all right, and just use TV value as being this two hundred fifteen instead of being minus
two hundred fifty.
So now, of course, our calculation would be wrong if we just ignore the minus.
So how can we actually fix our calculation now?
Because now if we check this.
It just ignores that it's a negative value, right?
So let's use the same example, ninety nine minus 66 is going to be now three, so it doesn't even display
the minus in front of it.
So how can we fix this?
Well, let's go here.
And.
Double check if.
Our prefix is not empty.
So that's what this exclamation mark is for.
And if it is.
Not empty.
Then I want to set one.
As being prefix plus one.
Which means that now it's going to be a negative value again, if it was of negative value before.
Why do they even do that?
Well, I do it in order to work around the error that we created earlier.
Right.
So this code and this code here, they're interlinked with each other, so to speak.
So first of all, we check if we have a minus at the start.
And if we do, then let's get rid of it.
So that's what this line does.
It just ignores this minus.
But then we already know that there is a prefix, which is a minus, and we can then go ahead and check
if it is an empty prefix.
If it's not an empty prefix, then add the prefix to whatever one was.
So whatever the first value was and then do the calculation.
And what that does is it then allows us to do the right calculation even if we work with the negative
value as a result.
All right.
And that's it, that's how subtraction works and now you can, of course, go ahead and do the same
thing with.
The other operations, so I hope you enjoyed this video, we touched upon a bunch of new things, starts
with substring splitting, all those things.
They're super useful when you work with the strings, as we do here, because the text that we get from
our text you is of type string.
So now we, of course, need to check if it works.
So let's run it.
And I'm going to calculate sixty six minus ninety nine and see if it works and see we get this negative
value.
So now I can do another calculation minus sixty six and you can see now I get minus ninety nine as the
result.
So it works.
Of course we still have the zero.
That is a little bit annoying.
I'd say we can get rid of it later on, but we only have the minus now.
So of course we need to implement all the other operations as well.
So I'd say see you in the next video where we're going to do that.