1 00:00:00,330 --> 00:00:01,440 ‫Welcome back. 2 00:00:01,470 --> 00:00:08,760 ‫In this video, we are going to have a look at how to parse from a string to an integer, for example. 3 00:00:08,760 --> 00:00:17,490 ‫So let's assume we have a string, which is my string, and it has a value of 15. 4 00:00:17,850 --> 00:00:21,960 ‫And now I want to use this 15 as a number. 5 00:00:21,990 --> 00:00:24,390 ‫So let's create another string. 6 00:00:24,570 --> 00:00:34,740 ‫My second string is going to be, let's say 13 now if I want to add those up, so let's say I want to 7 00:00:34,770 --> 00:00:36,750 ‫add 15 plus 13. 8 00:00:36,900 --> 00:00:46,620 ‫So string result is equal to my string plus my second string. 9 00:00:46,620 --> 00:00:49,620 ‫So one could assume that that would be the solution. 10 00:00:49,830 --> 00:00:58,050 ‫Now let's have a look at the console, because only that will show us the result and it's going to be 11 00:00:58,110 --> 00:00:59,100 ‫result. 12 00:00:59,400 --> 00:01:01,770 ‫And now console that read. 13 00:01:02,730 --> 00:01:05,070 ‫All right, so let's check that out. 14 00:01:05,100 --> 00:01:10,320 ‫Let's see what the console will tell us about the result of 15 plus 13. 15 00:01:11,220 --> 00:01:17,730 ‫And the result is 15 plus 13 or 1513, which is 1513. 16 00:01:17,730 --> 00:01:23,370 ‫And well, while that is great math, it doesn't work like that, or at least that's not the correct 17 00:01:23,370 --> 00:01:23,820 ‫math here. 18 00:01:23,820 --> 00:01:24,210 ‫Right. 19 00:01:24,210 --> 00:01:36,300 ‫So in order to fix that, we need to parse those strings to integers or to double or to other primitive 20 00:01:36,300 --> 00:01:37,860 ‫data types, which are numbers. 21 00:01:37,980 --> 00:01:42,390 ‫So now I need to go ahead and create an integer and I'm going to call that number one. 22 00:01:42,540 --> 00:01:52,140 ‫And in order to make an integer out of a string, I need to use int 32 dot and I can parse now. 23 00:01:52,170 --> 00:01:59,220 ‫So parse converts the string representation of a number to is 32 bits signed integer equivalent. 24 00:01:59,220 --> 00:02:03,720 ‫So that means it will create an integer out of the well. 25 00:02:03,720 --> 00:02:09,270 ‫For example, the my string, which is a 15 try parse however is a different version. 26 00:02:09,270 --> 00:02:14,910 ‫So as you can see converts the string representation of a number to is 32 bit signed integer equivalent. 27 00:02:15,060 --> 00:02:19,080 ‫A return value indicates whether the conversion succeeded. 28 00:02:19,080 --> 00:02:25,950 ‫So as you can see, we have a string that we need to hand over and we need to have a result if it didn't 29 00:02:25,950 --> 00:02:26,370 ‫work. 30 00:02:26,370 --> 00:02:28,830 ‫So we have parse and try parse. 31 00:02:28,830 --> 00:02:34,260 ‫So let's go ahead with Parse first and let's parse my string. 32 00:02:34,260 --> 00:02:37,740 ‫As you can see here in Parse, it requires a string. 33 00:02:38,190 --> 00:02:44,820 ‫So I'm just going to parse my string and I'm going to do the same thing with NUM two, which is also 34 00:02:44,820 --> 00:02:52,470 ‫going to be an N 32 and I'm going to pass it to my second string. 35 00:02:53,340 --> 00:02:53,700 ‫All right. 36 00:02:53,700 --> 00:03:03,630 ‫So now let's go ahead and create an end result instead of a string result, and that will be solved 37 00:03:03,630 --> 00:03:09,150 ‫int is num one plus num two and now let's print that out instead. 38 00:03:09,150 --> 00:03:15,480 ‫So let's print out the result int onto right line or onto our console. 39 00:03:15,780 --> 00:03:23,740 ‫So let's look and we see 28 is printed out and that's the result that I would expect if I add 15 to 40 00:03:23,740 --> 00:03:24,240 ‫13. 41 00:03:24,600 --> 00:03:25,200 ‫All right. 42 00:03:25,200 --> 00:03:29,280 ‫So I simply made integers out of strings and that's great. 43 00:03:29,670 --> 00:03:38,460 ‫So what you have to be here is super careful because as soon as this here is not passable or convertible 44 00:03:38,460 --> 00:03:42,210 ‫into an integer, we will get an exception. 45 00:03:42,210 --> 00:03:48,960 ‫So for example, if I add an A and here this 15 A is not going to be a number anymore, not an integer 46 00:03:48,960 --> 00:03:55,170 ‫anymore and we'll get an exception here on handled exception and we even get this Helloworld has stopped 47 00:03:55,170 --> 00:03:57,150 ‫working close program. 48 00:03:57,150 --> 00:04:03,420 ‫So that's something that you always have to be super careful with and we can handle that with either 49 00:04:03,450 --> 00:04:08,460 ‫a try and catch or we can handle it by using tri parse. 50 00:04:08,460 --> 00:04:13,890 ‫But those are both things that we are only going to use later on in the course when we will see how 51 00:04:13,890 --> 00:04:19,620 ‫to use, try and catch and when we will see how to use if statements because then we can use instead 52 00:04:19,620 --> 00:04:24,600 ‫of parse can use, try parse, but it will return an boolean. 53 00:04:24,600 --> 00:04:28,860 ‫So we will only find out if parsing worked or if it didn't work. 54 00:04:28,860 --> 00:04:34,230 ‫But whatever is with if is decision making and that's something that we're going to cover in a different 55 00:04:34,230 --> 00:04:34,860 ‫chapter. 56 00:04:34,860 --> 00:04:43,920 ‫But for now, just know that you can convert a string number into an integer or into, for example, 57 00:04:43,920 --> 00:04:45,000 ‫a double. 58 00:04:46,050 --> 00:04:49,040 ‫So double dot parse. 59 00:04:49,290 --> 00:04:51,090 ‫You can see you can do the same thing. 60 00:04:51,090 --> 00:04:55,680 ‫So you can parse strings into doubles and into other types as well. 61 00:04:56,460 --> 00:04:57,120 ‫Great. 62 00:04:57,120 --> 00:04:59,550 ‫So I'd say that's pretty good. 63 00:05:00,030 --> 00:05:07,620 ‫In the next video, we are going to have a little challenge and well, please go ahead and try to challenge. 64 00:05:07,620 --> 00:05:11,310 ‫And other than that, always be careful when trying to parse. 65 00:05:11,310 --> 00:05:16,470 ‫And if you really need parsing and you want to make sure that it really works, then go ahead with the 66 00:05:16,470 --> 00:05:18,630 ‫course and you will learn how to handle it. 67 00:05:18,630 --> 00:05:19,710 ‫So see you there.