1 00:00:00,630 --> 00:00:01,500 ‫Great. 2 00:00:01,530 --> 00:00:06,540 ‫No, you know how to create methods and you have proven that in the challenge. 3 00:00:06,540 --> 00:00:14,130 ‫So let's go ahead and see how we can use user input to run methods as well or to use our methods as 4 00:00:14,130 --> 00:00:14,580 ‫well. 5 00:00:14,580 --> 00:00:20,670 ‫And therefore we are going to have a look at how to get user input in general. 6 00:00:20,820 --> 00:00:30,390 ‫So I'm going to use a string and I'm going to call it input and this is going to be console dot and 7 00:00:30,390 --> 00:00:35,820 ‫we don't write something, we want to read something and that will be read line. 8 00:00:36,390 --> 00:00:37,170 ‫By that. 9 00:00:37,170 --> 00:00:43,410 ‫We prompt the user to read, to write something so that we can use it later on. 10 00:00:43,410 --> 00:00:46,590 ‫So that will be stored in a string called input. 11 00:00:46,650 --> 00:00:56,100 ‫And what we then can do is simply write that onto the console so we know what we have written and that 12 00:00:56,100 --> 00:01:02,520 ‫might not make that much sense yet, but this is just to see how it works in general. 13 00:01:02,550 --> 00:01:03,000 ‫All right. 14 00:01:03,000 --> 00:01:11,670 ‫So this is a very simple input program and we can put something into our command console. 15 00:01:11,670 --> 00:01:12,930 ‫So there it is. 16 00:01:13,260 --> 00:01:15,780 ‫It doesn't say that we should write something. 17 00:01:15,780 --> 00:01:17,760 ‫It's just waiting for us to type something. 18 00:01:17,760 --> 00:01:25,440 ‫So I'm going to type hello and it says hello and then I'm going to type. 19 00:01:25,440 --> 00:01:28,950 ‫Is somebody there? 20 00:01:30,180 --> 00:01:36,330 ‫And it shuts down so it's not done for endless loops or endless entries. 21 00:01:36,330 --> 00:01:40,650 ‫That's something that we are going to see later on when we use while loops. 22 00:01:40,650 --> 00:01:42,120 ‫But for now that's good. 23 00:01:42,120 --> 00:01:47,310 ‫So you know how to use one user input per line of code. 24 00:01:47,310 --> 00:01:53,640 ‫So this line of code takes the user input the second time we use it, it simply closes the program because 25 00:01:53,640 --> 00:01:54,660 ‫there's nothing else to do. 26 00:01:54,660 --> 00:02:03,510 ‫So it uses the user input, then it writes it and then it reads it onto the console and afterwards nothing 27 00:02:03,510 --> 00:02:04,530 ‫is happening. 28 00:02:04,530 --> 00:02:06,120 ‫So there is no code to execute. 29 00:02:06,120 --> 00:02:08,100 ‫So our program simply shuts down. 30 00:02:08,100 --> 00:02:12,060 ‫So now let's have a look at a adding tool. 31 00:02:12,060 --> 00:02:15,720 ‫So a little calculator which just does adding nothing else. 32 00:02:15,810 --> 00:02:20,250 ‫And yeah, for you as a little challenge, you could try to do that yourself. 33 00:02:20,250 --> 00:02:27,540 ‫So please go ahead and try to use that functionality that you see here to create a simple tool that 34 00:02:27,540 --> 00:02:31,050 ‫adds one plus one or whatever the user enters. 35 00:02:31,050 --> 00:02:36,420 ‫So the user input plus the user input and the user should be informed about what is to do. 36 00:02:36,540 --> 00:02:40,770 ‫So I know this is quite a challenging one, but please try to do that yourself. 37 00:02:43,850 --> 00:02:44,570 ‫All right. 38 00:02:44,570 --> 00:02:46,280 ‫I hope you tried it. 39 00:02:46,310 --> 00:02:51,790 ‫I hope you created a little method, and I'm going to create. 40 00:02:51,800 --> 00:02:55,460 ‫Well, you could have done it in the main method in general, but I'm going to create a little method 41 00:02:55,460 --> 00:03:07,820 ‫just because we are inside the methods chapter and I'm going to call it public static int and calculate. 42 00:03:09,350 --> 00:03:12,830 ‫This one is going to be an integer num one. 43 00:03:14,450 --> 00:03:16,610 ‫Actually, this is not going to be an integer. 44 00:03:16,610 --> 00:03:20,210 ‫It's going to simply return 45 00:03:22,700 --> 00:03:23,630 ‫a result. 46 00:03:25,190 --> 00:03:28,250 ‫And of course we need to create that result. 47 00:03:28,250 --> 00:03:34,700 ‫So I'm just going to create that result for now, just so we have it there and it's going to be five. 48 00:03:35,930 --> 00:03:44,690 ‫Now in here, what we want to have is the two entries of the user and we want to store them. 49 00:03:44,690 --> 00:03:52,610 ‫So first of all, we need to use right line so the user knows what he has to do. 50 00:03:52,610 --> 00:04:01,490 ‫Please enter the first number and then the user can enter the first number. 51 00:04:01,490 --> 00:04:11,870 ‫So this is going to be a string number one input and we use console dot read line. 52 00:04:12,170 --> 00:04:16,340 ‫So we read what the user types and then we do the same thing. 53 00:04:16,340 --> 00:04:19,190 ‫So we need to have the second number as well. 54 00:04:19,190 --> 00:04:31,400 ‫So console right line, please enter the second number and again we need to store it within a string 55 00:04:31,400 --> 00:04:36,380 ‫number two input which is console dot red line. 56 00:04:39,110 --> 00:04:41,900 ‫So now we have the two numbers. 57 00:04:42,020 --> 00:04:45,020 ‫What we now need to do is add them together. 58 00:04:45,020 --> 00:04:46,880 ‫But thing is they are strings. 59 00:04:46,880 --> 00:04:48,350 ‫So we cannot add them. 60 00:04:48,350 --> 00:04:50,000 ‫We cannot add strings to each other. 61 00:04:50,000 --> 00:04:53,360 ‫Well, we do or we can, but that's concatenation. 62 00:04:53,360 --> 00:04:54,710 ‫It's not really math here. 63 00:04:54,710 --> 00:05:01,970 ‫What we want to do is to get the mathematical result, and what we need here for is to convert them 64 00:05:01,970 --> 00:05:03,380 ‫into integers. 65 00:05:03,590 --> 00:05:07,760 ‫So I'm going to convert number one input. 66 00:05:08,700 --> 00:05:19,450 ‫Into a new variable which are called int num one, which is going to be int thought parse. 67 00:05:20,280 --> 00:05:22,020 ‫And what is it that we want to parse? 68 00:05:22,020 --> 00:05:25,200 ‫We want to parse number one input. 69 00:05:26,220 --> 00:05:33,630 ‫Now the same thing with the second number num two and parse and number two input. 70 00:05:34,170 --> 00:05:41,520 ‫So we want to make numbers out of what the user has entered and then we want to calculate the result 71 00:05:41,520 --> 00:05:44,490 ‫and actually we have the result variable here already. 72 00:05:44,490 --> 00:05:46,800 ‫That should be num one plus num two. 73 00:05:49,010 --> 00:05:49,550 ‫All right. 74 00:05:49,550 --> 00:05:57,320 ‫So now what is left to do is to actually write the result onto the console so we can do that right here 75 00:05:59,180 --> 00:06:07,070 ‫console or actually let's use this return because otherwise we wouldn't need the integer, so let's 76 00:06:07,070 --> 00:06:09,530 ‫use the result to write it here. 77 00:06:09,530 --> 00:06:22,760 ‫So what we do is we call our calculate method right here, and it's going to give us the return of this 78 00:06:22,760 --> 00:06:23,270 ‫method. 79 00:06:23,270 --> 00:06:28,820 ‫So whatever the user enters, we're going to calculate the result or the sum of them. 80 00:06:28,820 --> 00:06:29,780 ‫That's it. 81 00:06:29,780 --> 00:06:35,720 ‫So we won't need this input here anymore, but we will need the rest. 82 00:06:35,720 --> 00:06:39,950 ‫So now that we have that, let's check it out. 83 00:06:39,950 --> 00:06:41,870 ‫Let's see if it's working. 84 00:06:42,500 --> 00:06:44,810 ‫So please enter the first number. 85 00:06:44,990 --> 00:06:46,280 ‫I'm going to enter 25. 86 00:06:46,310 --> 00:06:53,900 ‫Please enter the second number, I'm going to enter 99 and I see the result of 124. 87 00:06:54,320 --> 00:07:01,730 ‫So now we have to be super careful because we do parsing here and we haven't seen decision making it. 88 00:07:01,730 --> 00:07:06,110 ‫So we don't know how to see if it worked or it didn't work. 89 00:07:06,110 --> 00:07:09,560 ‫Otherwise we could have used try pass or we could have used try and catch. 90 00:07:09,560 --> 00:07:11,780 ‫But that's something that we will handle later on. 91 00:07:11,780 --> 00:07:18,020 ‫For now, it's just important that you only enter numbers when you test that because as soon as you 92 00:07:18,020 --> 00:07:18,710 ‫don't. 93 00:07:18,710 --> 00:07:19,760 ‫So let's see. 94 00:07:19,760 --> 00:07:21,710 ‫Please enter the first number. 95 00:07:21,710 --> 00:07:29,570 ‫I'm going to use P and enter the second number I'm going to use I and our system crashes because it 96 00:07:29,570 --> 00:07:39,530 ‫says it can't pause and P or an AI into a number or into an integer input string was not in a correct 97 00:07:39,530 --> 00:07:40,160 ‫format. 98 00:07:40,280 --> 00:07:40,880 ‫All right. 99 00:07:40,880 --> 00:07:43,370 ‫But that's something as I said, we will handle that later on. 100 00:07:43,370 --> 00:07:48,890 ‫What's important now is that you see how you can read values from the user. 101 00:07:48,890 --> 00:07:54,200 ‫So that's something that we're going to use more and more throughout the course because user input is 102 00:07:54,200 --> 00:07:59,990 ‫much more interesting than just having it in here, just having some code in here which does things, 103 00:07:59,990 --> 00:08:03,590 ‫but it doesn't feel the same as you might understand. 104 00:08:03,740 --> 00:08:04,250 ‫Great. 105 00:08:04,250 --> 00:08:05,720 ‫So I hope you tried it. 106 00:08:05,720 --> 00:08:11,360 ‫I know it wasn't easy and this is not the only way to call this calculate method. 107 00:08:11,360 --> 00:08:22,400 ‫You could have also used the void instead here and not return a result, but instead simply write the 108 00:08:22,400 --> 00:08:27,380 ‫line here and give out the result. 109 00:08:28,700 --> 00:08:35,720 ‫So result like that and simply call the calculate method here. 110 00:08:36,140 --> 00:08:42,650 ‫By the way, let's stop that calc you light like that. 111 00:08:42,650 --> 00:08:44,570 ‫So that would have been the other solution. 112 00:08:44,570 --> 00:08:51,410 ‫Just to use a void method here, do the right line here and then just call the method there. 113 00:08:51,770 --> 00:08:52,070 ‫All right. 114 00:08:52,070 --> 00:08:54,080 ‫Those are multiple options. 115 00:08:54,200 --> 00:08:54,710 ‫Great. 116 00:08:54,710 --> 00:09:00,230 ‫So I'd say I'm going to save it here and see you in the next video.