‫Instructor: Hi, within this lecture,
‫we're going to continue building our calculator app.
‫So in order to do that,
‫I'm going to write the first method over here,
‫which is our sum method.
‫So first of all, we need to get the strings
‫or the numbers that our user has given as an input
‫and we need to add them together
‫and then finally, show the result
‫in a text view in the result text to the user.
‫We already have number1Text over here
‫and we can reach it under sum method, right?
‫Now, if I write something like this, number1Text,
‫as you can see, there is a method called getText.
‫And if you see the right side of it, it's editable.
‫So this is kind of a custom class.
‫It's not a string, it's not an integer,
‫it's not a data type that we are familiar with.
‫So you cannot edit,
‫you cannot do what you can do with strings.
‫It's a custom class but luckily,
‫we can convert it to string.
‫Now, we need to convert it into a string
‫so that we can do something with it.
‫We cannot do much with editable.
‫So this is a class in our class created
‫in order to write some text
‫or get some text out of the edit text.
‫So if you do that,
‫you can see toString method over here.
‫So this is what we are looking for.
‫This is how we get strings out of the edit text.
‫Now, I can actually make this equal
‫to into something called myString, for example.
‫And since this is a string,
‫I can get the other string as well
‫but I cannot add them together
‫because they're not numbers, right?
‫In order to do that, I can create some kind of an integer
‫or a double or a float out of those strings.
‫So you don't know how to do that.
‫I'm gonna show you, don't worry.
‫But you know, you get the logic, right?
‫You cannot add two strings together,
‫you can actually add them but it's nothing more
‫than concatenating two strings together,
‫like adding one next to each other.
‫But this is not what I want to do.
‫I want to add two numbers together.
‫So I have to create a number out of this string,
‫and you can actually convert one type into another
‫if they are suitable, if they're a fit for each other.
‫For example, if you have a string like five,
‫you can convert it into an integer.
‫So in order to do that,
‫I'm going to use Integer class like this.
‫So write Integer and if you say .parse,
‫you can see parseInt method
‫and it actually requires a string as a parameter.
‫So this is what we are going to use.
‫We're going to use this parseInt method
‫and we're going to give our string as a parameter.
‫It will convert our string into an integer.
‫Okay? And we don't have to do that anymore.
‫So let me delete everything from here,
‫and start by writing Integer.parseInt.
‫It will ask me for this string.
‫I'm going to cut this from here.
‫I'm not gonna take the semicolon,
‫and add it over here.
‫And this is what we're gonna use in order
‫to get an integer out of our editText.
‫So I'm going to assign this to a variable called number1.
‫And of course, we can do that for number2 as well.
‫Right?
‫Since we have two numbers, this is number one.
‫I'm gonna get the second one as well.
‫Now, I'm gonna copy this and paste it over here.
‫Change it to number two,
‫and of course, I'm gonna change the text one as well
‫so that I will have two numbers.
‫Now, I can actually create a result integer
‫and add them together like number1 plus number2,
‫and hit semicolon.
‫Here you go.
‫Now I see the final result over here.
‫Now, I can actually change the resultText
‫by saying resultText.setText.
‫And when you say setText,
‫it actually asks for a string or a character.
‫You cannot just come over here
‫and give result as a parameter
‫because it's an integer.
‫It will look like it's working
‫but it's gonna crash once you do that.
‫So you have to convert this integer into a string.
‫And luckily, we have a very good method in order to do that.
‫Once you write something like this,
‫write any string over here like this,
‫you can add another integer
‫or another float or another number
‫and it will convert it into a string.
‫Now this is okay.
‫You can actually test this.
‫So result plus result variable.
‫Okay?
‫So I'm going to test this on my emulator, okay?
‫So I suggest you do the same thing as well.
‫So open the emulator
‫and just write some random numbers and hit them together.
‫Here you go, result is eight.
‫So we can try with other numbers like 13.
‫Let's change it to 3,000, for example.
‫Here you go. Now it's working.
‫Of course, it's trivial to do the others as well.
‫I'm just gonna copy them
‫and paste them over here, okay?
‫And paste them over here and paste them over here.
‫So right now I can just change the sign
‫like this is deduction,
‫this is multiply and this is divide.
‫Here you go.
‫Now we are ready to test this.
‫So we can see the other methods,
‫other operations as well.
‫So I believe we're coming to an end
‫but actually, we have some more things to do,
‫some more things to consider.
‫So let me try this.
‫So this is 12. This is eight.
‫This is 20 and this is 5.
‫So here you go, it's now working.
‫Of course, we have some kind of issues in this app.
‫Since we're doing this for a test,
‫it's not very important
‫but it's important to practice
‫what we have learned so far.
‫For example, if you just write your name
‫for an input in your first edit text,
‫if you click any of the buttons, it will crash.
‫And whilst you see a screen like this,
‫you can go to Logcat immediately
‫in order to see why it's crashed.
‫So if you go to error section,
‫you can find the real error message
‫that you're looking for.
‫So here you go.
‫One of the error messages.
‫So it says that yeah, I cannot do this onClick because why?
‫Because there is something wrong, as you can see.
‫We have an input string of atil
‫and we tried to cast this
‫as an integer like Integer.parseInt
‫and it won't work obviously
‫because it's not a number, it's just a random string,
‫a random text.
‫So how do we overcome this problem?
‫This is one of the things that we should actually consider
‫when we make calculator or make any apps.
‫So we're going to do that within the next lecture.