Come back in this video, we are going to implement the subtraction functionality and therefore we are
going to use to split and the substring methods on strings.
So this is on one hand, the calculator that we're building.
But actually what it is, it's a little tutorial teaching you how to work with strings because you've
learned quite a bit about strings already.
For example, this contains which returns a Boolean, right?
By the way, if you want to know more about any given method, you can hover over it and it will tell
you what it will return.
So this contains, as we know, returns a Boolean.
And now we're going to see how we can split a string because that's what we need to do.
So let's say with 99 problems and we are reducing one, then we have 99 minus one here and this should
give me 88.
But I, of course, haven't implemented the code for the equal sign yet.
Well, you see, it just displays a bunch of equals, but that's not what we want.
We actually want to then see what is coming before the operator.
And what is coming after the operator.
So we need to split the string that we have at the operator position and we need to then do the mathematical
operations OK.
And we're going to start with subtraction.
And in the next video, we're going to add the final missing operations.
Therefore, what I'm going to start with is going to be a new method, and the new methods function
that I'm going to create is going to be on equal.
So this will be executed at once.
We click on the equal button and as we know with buttons, we need to pass a view so that we know which
button was pressed and we can then use that and so forth.
So here what we need.
First to check is once we click on the equal button, we need to know if the last value that was entered
was in fact numeric, because we don't want to have the situation where we enter 99 minus and then we
press the equals sign that it does a calculation because that wouldn't work.
There is nothing to calculate.
So we need to make sure that the final statement was in fact a number or the final entry was numeric.
And how do we find that out?
A little hint.
We have this variable here called last numeric.
OK, so we can check if last numeric is true.
And only if that's the case, then we want to load the TextView value.
So the value that is inside of this texture that we have here, what we see now 99 minus one, and we
can do that by creating a variable TV value will be our text view input.
And because it is a noble I need to add the question mark here.
Text to string what that will do is it will take the text view input and give us a text and then make
a string out of it because I want to give value to be an actual string and not to be a text because
a text by itself is just a different type of detail.
So here text is a char sequence and we actually need it to be of type string.
All right.
So now the thing that we do next can go wrong so it could crash our application and then order for it
not to crash our application.
We can use a try and catch block.
So here to try and catch will look like this.
We have to try statement and then we have the catch statement.
And here we need to say which kind of error we could have, and I'm going to use the arithmetic exception.
What that will do is it will give us an exception that will be of type a romantic exception, which
just means that it is going to catch things such as dividing by zero or just calculations that won't
work on the mathematical level and arithmetic level.
OK, so here what I want to do is I want to print that on the stack trace so that we'll just print it
onto our locket if we ever get an error here.
OK, we're going to see you a lot more about LOLcats later on.
For now, what's important is this tribal part here.
This is where we want to execute the code and the code that we want to execute is going to be the actual
splitting of values and doing the calculations and stuff.
So what I will set up here for is going to be a new variable and I'm going to call this one split value.
And what it will take is our TV value, which is this text you hear and it will split it.
So there's a split method at the split method exists for strings.
OK, so here we now need to go ahead and say what we want to split.
You can see that there are different types of parameters that you can pass.
You can use of an entire rhetorics pattern.
So a regular expression pattern, it would go too far for this part of the course.
But regular expressions just allows you to filter text very easily.
And what we're going to use is going to be this Vaart limiters with this string.
So we are limiting or using the item minus to split our text.
So what we will do here is we will split this TV value.
So let's say we had this string of 99 minus one.
Then what it will do is it will split it up where the first entry will be 99 nine and the second entry
will be one.
OK.
And it will do that in form of an R rate.
OK, so how we can actually use it is the following way.
I'm going to just create two variables here.
I'm going to create one with the name one, and it will be at position zero and the second one, which
will be to put which will be split value at position one.
OK.
So as I stated, this one here, for example, would then be the 99, and this too would be the one,
for example.
All right.
So that's how we can split a string by using this split method.
It exists in the string class so you can check it out.
It takes a char sequence and splits it with this, the limiter that we are passing.
OK, and then what we can now do is we can use that to fill our text view input again.
So here we can just tap the text to be.
And as text view input is another tool, we need to add the question mark here and we can just go ahead
and say one and convert it into a double.
Because one now is going to be of type string, OK, because we are splitting a string which will return
to strings.
So this is going to be a string entry and this will be a string entry.
And then we are using that as a double.
We are subtracting two as a double.
So we need to convert that into a double as well.
Like so, but this by itself will of course not work because text requires a char sequence that we assign
to it, which means it requires a string kind of format to be assigned to it.
So how can we make this into a string?
Well, we could, of course, create a new variable here result, and we could say it's going to be
those two and then we could just go ahead and use result to string, and that would do the trick.
OK, so that's how we can do it in two lines a shorter way or a way.
How we do it in one line would just be to use those and surround them with brackets.
So now we have opening bracket one to double minus two to double closing bracket.
And now this entire thing here is treated like a double.
So just like a number and we're converting that into a string by adding the dot after the closing bracket
and making a string out of it, which will now make a number into a text kind of format.
Again, which is what this TV input our text input requires in form of its format.
All right, so now let's test this, and in order to test it, we need to go over to our equals sign,
double click on it, and then it will jump to the button that has the name button equal.
And now we can go ahead.
And actually, instead of using the on digit method, we use the on Eq. method.
So this one will now be executed, which means this now turns from gray into yellow, which means it
is used.
So now we are using this on equal method, which will check if the last entry was a number and then
if it was, it goes through all of this quote.
So now this will crash our program if we don't use it correctly.
But now let's first of all, test if we can subtract something and we could see 99 minus one is ninety
eight point zero, which is the double representation of 98.
OK, so it's not 98, but it's ninety eight point zero because here we're talking about doubles.
If we were to convert them into interest and we wouldn't see this dot zero.
All right.
So that is how we get the first part of it running.
But now let's say we add a minus.
And we press to enter key.
Well, you see, it doesn't break anything because we have the split value one just being empty.
So this calculation is just saying one to double minus nothing and it just stays where it is.
OK, so now let's see how we could crush this solution.
So let's try to take sixty six and subtract 99 from it.
Now we get this negative value.
So now let's go ahead and subtract another 66 from it and you see our program crashes.
So you can still see this other program, which is, well, just another instance of this program that
I have open on my other screen.
But this basically is still a problem because it just doesn't get it that we have a minus at the beginning.
And then it's trying to do something that it just cannot do because it splits it at this minus rate.
So if the value at the beginning you cannot enter minus is so if this value at the beginning is minus
something, what it will try to do is it will take the part that is left of the minus, which is nothing
minus the thing that is right of the minus, which could be something.
So now basically, our program just doesn't know what it should do, and it keeps stopping, so it crashes.
And now how can we get around it?
Well, first of all, we need to add a prefix.
So here we're going to create the new variable called prefix, which.
Will be an empty string at that time.
And then instead of our tribe block here, we can check if our TV value.
Starts with a minus.
So with the starts with method and we check, does it start with a mine at minus?
So does it start with this prefix minus?
And then if that is the case, we can just go ahead and say that the prefix will in fact be a minus.
And then we can take the TV value and set it up accordingly.
Which I will do with TV value, but substring at position one.
So now you might say, holy smokes, what's going on here, Dennis?
Well, it's I'm using the TV value here.
The old TV value.
And I'm saying use the substring ad position one, which basically just means get rid of the first entry
that you have in the string.
So if the string TBE value was, let's say, minus 99 at the beginning, then after this statement here
it will just be 99.
Because what we're doing is we create a substring out of our TV's value, which starts at index one
of the string.
So that means there's minus is index zero and this is index one.
So it starts at index one, which means it starts at 99 and just does create the rest of the string.
Just to show you this real quick on in Scotland playground here, what you can see is I'm creating this
via my string, which says Hello world, and then I'm using the substring method at position one and
then print my string.
So I override the old string with the ball substring of the old string.
And you see, I get hello world without the age, which is just Helloworld.
So if we were to use this example with the 99 and then we run it again, the minus 99 we can see, we
just get 99 because it just get rid gets rid of this first entry.
That's what you can do with this substring.
By the way, if we were to use Helloworld and we say run at entry five, it will just split world with
an empty value before that, because that's zero.
One two three four.
And then it starts at five, which means it adds, there's empty space.
You can see that here.
If you market, it has an empty space.
So that's what the substring does for us.
So now we have this prefix, right, so that's cool and all, but we're never using it.
Now let's actually use it and where would be a good spot to use it?
Well, the thing is we need to check if our string contains a minus.
So here, TV value contains a minus.
And if that's the case, then let's run all of this code here.
So let me put that in there.
But then on top of that, we need to assign the prefix back to the one because what we just did up here
is we deleted the minus.
And if there was a minus in our first entry, so in the 99 year, if there was a minus in 99 at this
point, it doesn't have the minus, even though it had it before, and we just deleted it.
So we stored that.
We deleted it in this prefix variable, and now we can add it back by checking if prefix is not empty.
So here is not empty.
If that's the case, then just use the old one and add prefix to it.
So the new one should be prefix plus one, because this is a string and prefix is a string, we can
just add those together.
So if one was ninety nine and the prefix was minus, now one will be minus ninety nine.
And now our calculation should work flawlessly, even with subtractions.
So let's see it.
Sixty six minus ninety nine and now minus 66.
And we can see it works flawlessly and we can subtract even with negative numbers.
All right, so at this point, we just need to add the missing operations, and that's what we're going
to add in the next video.
So see you there.