‫Instructor: Hi.
‫Within this lecture we are going to finalize
‫our calculator app by making it into a Safe calculator.
‫Now we get this error
‫because we did give something odd over here
‫like an apple or a till or any word rather than a number.
‫Okay? So you can just try writing any words and it'll crash.
‫If you don't give any numbers over here, it will crash.
‫So one of the things that you may consider
‫is to change this keyboard so that user will be able to
‫only give numbers as an input, right?
‫And there's a fine way to do that
‫if you come over here to your activity main xml, okay?
‫If you click one of the edit texts
‫now you can find something called input type.
‫If you cannot find it over here you can search it from there
‫and you can find it on the right hand side like this.
‫So find input type.
‫And as you can see the current input type we have over here
‫is the text person name.
‫So this changes the keyboard.
‫So we can make it for passwords, we can make it for emails
‫we can make it for numbers as well.
‫So make sure you check the numbers
‫and uncheck the text person name.
‫So the input type is only number right now.
‫So it'll display the number keyboard.
‫Of course, we are gonna have to do the same thing
‫for the edit text two as well.
‫So let me uncheck this,
‫but once I unchecked that, it's disappears.
‫So I'm gonna search it from here, okay
‫input type and come over here and find the number.
‫And I'm going to make it true
‫so that it will display only the number.
‫So here you go.
‫I believe we are now okay to test this.
‫So let me run this from scratch
‫and let me go back into my coding section.
‫So if you come over here
‫and click one of the edit texts right now, as you can see
‫we can only see the numbers right now.
‫So I can give the inputs as I want.
‫However, there's some still kind of an issue
‫that we might need to consider like this.
‫So if a user leaves it blank
‫and just clicks one of the buttons it'll crash again
‫because there won't be any input
‫and it won't be able to convert it into a number or stuff.
‫So it will crash again.
‫So one of the things that we can do over here
‫is to consider checking to see if the input is empty or not.
‫So how do we do that?
‫Of course, we can use an if control, if statement, right?
‫Like we have seen before.
‫So before we do everything over here,
‫under some for example, we can check to see
‫if this toString methods give us something
‫like an empty string.
‫And if that's the case, we're not gonna do anything else.
‫For example, we can come over here
‫and say if numberText.getText.toString
‫and I believe you have a tendency to do like
‫this double equal sign and then an empty string.
‫And it's a very good thought, but it doesn't work in Java.
‫It works in Kotlin, it works in Python.
‫It works in Swift, but it doesn't work in Java
‫because you cannot actually check to see
‫if a string equals to some string with the sign
‫but rather you have to say toString.matches, okay?
‫So you check if the toString are the same
‫or have the same content with this matches method.
‫So once you do that,
‫you can actually write whatever you want
‫inside of this matches method like this.
‫You can just add double quotation marks over here.
‫It will search for a string, empty string.
‫And if that's the case, we are not gonna do anything else.
‫However, we only did it for number one text.
‫Now we're gonna have to do it for number two texts as well.
‫So we are going to use an OR open door here
‫rather than ands,
‫because either of this is true, then there's a problem.
‫So we're going to say if number one text is empty
‫or number two text is empty, then don't do anything.
‫For example, you can do the pipe signs over here
‫like we have seen before, and it means or
‫and you can add number two text
‫like number two text.getText.toString.matches"
‫So let me close this down, and here you go.
‫Now we are ready.
‫So let me delete the space and open my curly braces.
‫So if there is a problem,
‫then we are going to display some kind of message.
‫But else, if there is no problem,
‫I'm going to cut everything from here and paste them here
‫but for a reason, I cannot do that.
‫Let me try one more time.
‫Here you go, now I managed to do it.
‫So if there is a problem, I'm not gonna do those
‫but if there isn't one, then I'm gonna do those.
‫And if there isn't any, there is a problem actually
‫we can just use the result text.set text
‫in order to give a message to the user
‫like there is a no number, enter number, what are you doing?
‫Something like this.
‫Okay?
‫So let me make this
‫into an uppercase letter like this, and here you go.
‫Now let's test this with some method.
‫And if it works,
‫of course the rest of the app will be trivial to complete
‫like I'm gonna copy and paste everything.
‫So let me try with 10
‫or let me leave this blank and give 10 over here,
‫and here you go.
‫It says enter number.
‫So if I write something like two or 20
‫then it'll just do it.
‫And if I leave number two blank as well
‫I will get the error message.
‫So let me copy this and let me delete everything
‫under deduct and paste the new one and just change the sign.
‫Okay, like this.
‫And here you go.
‫So let me do that for multiply
‫let me just change design, and let me do that
‫for the final method as well, which is divide.
‫Come over here and change design.
‫Here you go.
‫Now I believe we completed our app.
‫Let's try this.
‫Of course, there are other things to consider
‫if you are really developing a calculator for real world
‫like if you just divide a number by zero
‫it will cause some problems.
‫Maybe you may want to check for that as well
‫but I'm not gonna do that.
‫We are just doing this as a practice.
‫If you can always that problem's on the way,
‫then I believe you can actually solve them
‫by yourselves as well.
‫So let me test this.
‫Okay I'm gonna say 10 and leave those blank
‫and as you can see, it says enter number
‫but once I give some kind of input over here, like two
‫then it will display the results.
‫So now it works.
‫And we have learned how to work with Java fundamentals.
‫You have learned how to deal with problems,
‫where to look at when you get a crash,
‫and how to actually think
‫consider the problems that may occur
‫during the user input section
‫and have some kind of preventive methods for that as well.
‫This is how you deal with Java coding.
‫This is how we deal with Android development in general.
‫So I hope you enjoyed this section.
‫Let's meet at the new one.