1 00:00:00,780 --> 00:00:01,650 ‫Instructor: Hi. 2 00:00:01,650 --> 00:00:04,170 ‫Within this lecture we are going to finalize 3 00:00:04,170 --> 00:00:08,310 ‫our calculator app by making it into a Safe calculator. 4 00:00:08,310 --> 00:00:10,080 ‫Now we get this error 5 00:00:10,080 --> 00:00:14,220 ‫because we did give something odd over here 6 00:00:14,220 --> 00:00:19,220 ‫like an apple or a till or any word rather than a number. 7 00:00:20,100 --> 00:00:25,100 ‫Okay? So you can just try writing any words and it'll crash. 8 00:00:25,590 --> 00:00:29,550 ‫If you don't give any numbers over here, it will crash. 9 00:00:29,550 --> 00:00:32,460 ‫So one of the things that you may consider 10 00:00:32,460 --> 00:00:36,690 ‫is to change this keyboard so that user will be able to 11 00:00:36,690 --> 00:00:40,230 ‫only give numbers as an input, right? 12 00:00:40,230 --> 00:00:42,540 ‫And there's a fine way to do that 13 00:00:42,540 --> 00:00:46,410 ‫if you come over here to your activity main xml, okay? 14 00:00:46,410 --> 00:00:49,260 ‫If you click one of the edit texts 15 00:00:49,260 --> 00:00:53,820 ‫now you can find something called input type. 16 00:00:53,820 --> 00:00:57,570 ‫If you cannot find it over here you can search it from there 17 00:00:57,570 --> 00:01:01,380 ‫and you can find it on the right hand side like this. 18 00:01:01,380 --> 00:01:02,940 ‫So find input type. 19 00:01:02,940 --> 00:01:06,390 ‫And as you can see the current input type we have over here 20 00:01:06,390 --> 00:01:09,270 ‫is the text person name. 21 00:01:09,270 --> 00:01:12,840 ‫So this changes the keyboard. 22 00:01:12,840 --> 00:01:16,800 ‫So we can make it for passwords, we can make it for emails 23 00:01:16,800 --> 00:01:19,800 ‫we can make it for numbers as well. 24 00:01:19,800 --> 00:01:21,360 ‫So make sure you check the numbers 25 00:01:21,360 --> 00:01:24,510 ‫and uncheck the text person name. 26 00:01:24,510 --> 00:01:28,170 ‫So the input type is only number right now. 27 00:01:28,170 --> 00:01:30,753 ‫So it'll display the number keyboard. 28 00:01:31,680 --> 00:01:33,870 ‫Of course, we are gonna have to do the same thing 29 00:01:33,870 --> 00:01:36,270 ‫for the edit text two as well. 30 00:01:36,270 --> 00:01:38,460 ‫So let me uncheck this, 31 00:01:38,460 --> 00:01:42,180 ‫but once I unchecked that, it's disappears. 32 00:01:42,180 --> 00:01:45,120 ‫So I'm gonna search it from here, okay 33 00:01:45,120 --> 00:01:49,260 ‫input type and come over here and find the number. 34 00:01:49,260 --> 00:01:50,880 ‫And I'm going to make it true 35 00:01:50,880 --> 00:01:53,730 ‫so that it will display only the number. 36 00:01:53,730 --> 00:01:55,512 ‫So here you go. 37 00:01:55,512 --> 00:01:58,050 ‫I believe we are now okay to test this. 38 00:01:58,050 --> 00:02:01,080 ‫So let me run this from scratch 39 00:02:01,080 --> 00:02:04,290 ‫and let me go back into my coding section. 40 00:02:04,290 --> 00:02:05,820 ‫So if you come over here 41 00:02:05,820 --> 00:02:08,730 ‫and click one of the edit texts right now, as you can see 42 00:02:08,730 --> 00:02:11,880 ‫we can only see the numbers right now. 43 00:02:11,880 --> 00:02:15,870 ‫So I can give the inputs as I want. 44 00:02:15,870 --> 00:02:19,800 ‫However, there's some still kind of an issue 45 00:02:19,800 --> 00:02:23,220 ‫that we might need to consider like this. 46 00:02:23,220 --> 00:02:26,760 ‫So if a user leaves it blank 47 00:02:26,760 --> 00:02:30,390 ‫and just clicks one of the buttons it'll crash again 48 00:02:30,390 --> 00:02:32,340 ‫because there won't be any input 49 00:02:32,340 --> 00:02:37,080 ‫and it won't be able to convert it into a number or stuff. 50 00:02:37,080 --> 00:02:38,703 ‫So it will crash again. 51 00:02:39,690 --> 00:02:42,930 ‫So one of the things that we can do over here 52 00:02:42,930 --> 00:02:47,930 ‫is to consider checking to see if the input is empty or not. 53 00:02:50,640 --> 00:02:53,100 ‫So how do we do that? 54 00:02:53,100 --> 00:02:57,060 ‫Of course, we can use an if control, if statement, right? 55 00:02:57,060 --> 00:02:59,160 ‫Like we have seen before. 56 00:02:59,160 --> 00:03:02,220 ‫So before we do everything over here, 57 00:03:02,220 --> 00:03:05,610 ‫under some for example, we can check to see 58 00:03:05,610 --> 00:03:10,610 ‫if this toString methods give us something 59 00:03:11,160 --> 00:03:12,720 ‫like an empty string. 60 00:03:12,720 --> 00:03:16,740 ‫And if that's the case, we're not gonna do anything else. 61 00:03:16,740 --> 00:03:18,750 ‫For example, we can come over here 62 00:03:18,750 --> 00:03:21,730 ‫and say if numberText.getText.toString 63 00:03:23,820 --> 00:03:27,990 ‫and I believe you have a tendency to do like 64 00:03:27,990 --> 00:03:32,990 ‫this double equal sign and then an empty string. 65 00:03:33,630 --> 00:03:38,190 ‫And it's a very good thought, but it doesn't work in Java. 66 00:03:38,190 --> 00:03:40,770 ‫It works in Kotlin, it works in Python. 67 00:03:40,770 --> 00:03:43,960 ‫It works in Swift, but it doesn't work in Java 68 00:03:44,820 --> 00:03:48,270 ‫because you cannot actually check to see 69 00:03:48,270 --> 00:03:52,980 ‫if a string equals to some string with the sign 70 00:03:52,980 --> 00:03:57,980 ‫but rather you have to say toString.matches, okay? 71 00:03:58,890 --> 00:04:02,910 ‫So you check if the toString are the same 72 00:04:02,910 --> 00:04:07,230 ‫or have the same content with this matches method. 73 00:04:07,230 --> 00:04:09,450 ‫So once you do that, 74 00:04:09,450 --> 00:04:12,660 ‫you can actually write whatever you want 75 00:04:12,660 --> 00:04:16,560 ‫inside of this matches method like this. 76 00:04:16,560 --> 00:04:20,730 ‫You can just add double quotation marks over here. 77 00:04:20,730 --> 00:04:23,730 ‫It will search for a string, empty string. 78 00:04:23,730 --> 00:04:27,423 ‫And if that's the case, we are not gonna do anything else. 79 00:04:28,320 --> 00:04:32,070 ‫However, we only did it for number one text. 80 00:04:32,070 --> 00:04:35,400 ‫Now we're gonna have to do it for number two texts as well. 81 00:04:35,400 --> 00:04:38,730 ‫So we are going to use an OR open door here 82 00:04:38,730 --> 00:04:40,200 ‫rather than ands, 83 00:04:40,200 --> 00:04:45,200 ‫because either of this is true, then there's a problem. 84 00:04:45,870 --> 00:04:50,070 ‫So we're going to say if number one text is empty 85 00:04:50,070 --> 00:04:55,070 ‫or number two text is empty, then don't do anything. 86 00:04:55,350 --> 00:04:58,680 ‫For example, you can do the pipe signs over here 87 00:04:58,680 --> 00:05:02,370 ‫like we have seen before, and it means or 88 00:05:02,370 --> 00:05:04,890 ‫and you can add number two text 89 00:05:04,890 --> 00:05:08,793 ‫like number two text.getText.toString.matches" 90 00:05:11,520 --> 00:05:14,250 ‫So let me close this down, and here you go. 91 00:05:14,250 --> 00:05:16,080 ‫Now we are ready. 92 00:05:16,080 --> 00:05:20,970 ‫So let me delete the space and open my curly braces. 93 00:05:20,970 --> 00:05:23,790 ‫So if there is a problem, 94 00:05:23,790 --> 00:05:26,250 ‫then we are going to display some kind of message. 95 00:05:26,250 --> 00:05:28,770 ‫But else, if there is no problem, 96 00:05:28,770 --> 00:05:33,540 ‫I'm going to cut everything from here and paste them here 97 00:05:33,540 --> 00:05:36,120 ‫but for a reason, I cannot do that. 98 00:05:36,120 --> 00:05:37,830 ‫Let me try one more time. 99 00:05:37,830 --> 00:05:40,620 ‫Here you go, now I managed to do it. 100 00:05:40,620 --> 00:05:43,680 ‫So if there is a problem, I'm not gonna do those 101 00:05:43,680 --> 00:05:46,800 ‫but if there isn't one, then I'm gonna do those. 102 00:05:46,800 --> 00:05:50,040 ‫And if there isn't any, there is a problem actually 103 00:05:50,040 --> 00:05:54,750 ‫we can just use the result text.set text 104 00:05:54,750 --> 00:05:56,730 ‫in order to give a message to the user 105 00:05:56,730 --> 00:06:01,530 ‫like there is a no number, enter number, what are you doing? 106 00:06:01,530 --> 00:06:03,120 ‫Something like this. 107 00:06:03,120 --> 00:06:04,410 ‫Okay? 108 00:06:04,410 --> 00:06:06,240 ‫So let me make this 109 00:06:06,240 --> 00:06:10,650 ‫into an uppercase letter like this, and here you go. 110 00:06:10,650 --> 00:06:13,500 ‫Now let's test this with some method. 111 00:06:13,500 --> 00:06:14,550 ‫And if it works, 112 00:06:14,550 --> 00:06:18,840 ‫of course the rest of the app will be trivial to complete 113 00:06:18,840 --> 00:06:21,450 ‫like I'm gonna copy and paste everything. 114 00:06:21,450 --> 00:06:24,240 ‫So let me try with 10 115 00:06:24,240 --> 00:06:27,990 ‫or let me leave this blank and give 10 over here, 116 00:06:27,990 --> 00:06:28,823 ‫and here you go. 117 00:06:28,823 --> 00:06:30,930 ‫It says enter number. 118 00:06:30,930 --> 00:06:35,400 ‫So if I write something like two or 20 119 00:06:35,400 --> 00:06:37,590 ‫then it'll just do it. 120 00:06:37,590 --> 00:06:40,110 ‫And if I leave number two blank as well 121 00:06:40,110 --> 00:06:42,420 ‫I will get the error message. 122 00:06:42,420 --> 00:06:46,560 ‫So let me copy this and let me delete everything 123 00:06:46,560 --> 00:06:51,450 ‫under deduct and paste the new one and just change the sign. 124 00:06:51,450 --> 00:06:53,610 ‫Okay, like this. 125 00:06:53,610 --> 00:06:54,990 ‫And here you go. 126 00:06:54,990 --> 00:06:58,440 ‫So let me do that for multiply 127 00:06:58,440 --> 00:07:02,340 ‫let me just change design, and let me do that 128 00:07:02,340 --> 00:07:06,150 ‫for the final method as well, which is divide. 129 00:07:06,150 --> 00:07:09,180 ‫Come over here and change design. 130 00:07:09,180 --> 00:07:10,200 ‫Here you go. 131 00:07:10,200 --> 00:07:13,680 ‫Now I believe we completed our app. 132 00:07:13,680 --> 00:07:15,870 ‫Let's try this. 133 00:07:15,870 --> 00:07:18,120 ‫Of course, there are other things to consider 134 00:07:18,120 --> 00:07:22,680 ‫if you are really developing a calculator for real world 135 00:07:22,680 --> 00:07:27,090 ‫like if you just divide a number by zero 136 00:07:27,090 --> 00:07:28,860 ‫it will cause some problems. 137 00:07:28,860 --> 00:07:31,140 ‫Maybe you may want to check for that as well 138 00:07:31,140 --> 00:07:32,400 ‫but I'm not gonna do that. 139 00:07:32,400 --> 00:07:35,430 ‫We are just doing this as a practice. 140 00:07:35,430 --> 00:07:38,940 ‫If you can always that problem's on the way, 141 00:07:38,940 --> 00:07:42,150 ‫then I believe you can actually solve them 142 00:07:42,150 --> 00:07:44,070 ‫by yourselves as well. 143 00:07:44,070 --> 00:07:45,990 ‫So let me test this. 144 00:07:45,990 --> 00:07:49,890 ‫Okay I'm gonna say 10 and leave those blank 145 00:07:49,890 --> 00:07:52,350 ‫and as you can see, it says enter number 146 00:07:52,350 --> 00:07:56,040 ‫but once I give some kind of input over here, like two 147 00:07:56,040 --> 00:07:58,380 ‫then it will display the results. 148 00:07:58,380 --> 00:08:00,570 ‫So now it works. 149 00:08:00,570 --> 00:08:04,050 ‫And we have learned how to work with Java fundamentals. 150 00:08:04,050 --> 00:08:06,750 ‫You have learned how to deal with problems, 151 00:08:06,750 --> 00:08:09,240 ‫where to look at when you get a crash, 152 00:08:09,240 --> 00:08:12,870 ‫and how to actually think 153 00:08:12,870 --> 00:08:15,870 ‫consider the problems that may occur 154 00:08:15,870 --> 00:08:19,380 ‫during the user input section 155 00:08:19,380 --> 00:08:24,240 ‫and have some kind of preventive methods for that as well. 156 00:08:24,240 --> 00:08:27,210 ‫This is how you deal with Java coding. 157 00:08:27,210 --> 00:08:30,660 ‫This is how we deal with Android development in general. 158 00:08:30,660 --> 00:08:33,150 ‫So I hope you enjoyed this section. 159 00:08:33,150 --> 00:08:35,463 ‫Let's meet at the new one.