‫Welcome back.
‫In this video, we are going to have a look at how to parse from a string to an integer, for example.
‫So let's assume we have a string, which is my string, and it has a value of 15.
‫And now I want to use this 15 as a number.
‫So let's create another string.
‫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
‫add 15 plus 13.
‫So string result is equal to my string plus my second string.
‫So one could assume that that would be the solution.
‫Now let's have a look at the console, because only that will show us the result and it's going to be
‫result.
‫And now console that read.
‫All right, so let's check that out.
‫Let's see what the console will tell us about the result of 15 plus 13.
‫And the result is 15 plus 13 or 1513, which is 1513.
‫And well, while that is great math, it doesn't work like that, or at least that's not the correct
‫math here.
‫Right.
‫So in order to fix that, we need to parse those strings to integers or to double or to other primitive
‫data types, which are numbers.
‫So now I need to go ahead and create an integer and I'm going to call that number one.
‫And in order to make an integer out of a string, I need to use int 32 dot and I can parse now.
‫So parse converts the string representation of a number to is 32 bits signed integer equivalent.
‫So that means it will create an integer out of the well.
‫For example, the my string, which is a 15 try parse however is a different version.
‫So as you can see converts the string representation of a number to is 32 bit signed integer equivalent.
‫A return value indicates whether the conversion succeeded.
‫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
‫work.
‫So we have parse and try parse.
‫So let's go ahead with Parse first and let's parse my string.
‫As you can see here in Parse, it requires a string.
‫So I'm just going to parse my string and I'm going to do the same thing with NUM two, which is also
‫going to be an N 32 and I'm going to pass it to my second string.
‫All right.
‫So now let's go ahead and create an end result instead of a string result, and that will be solved
‫int is num one plus num two and now let's print that out instead.
‫So let's print out the result int onto right line or onto our console.
‫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
‫13.
‫All right.
‫So I simply made integers out of strings and that's great.
‫So what you have to be here is super careful because as soon as this here is not passable or convertible
‫into an integer, we will get an exception.
‫So for example, if I add an A and here this 15 A is not going to be a number anymore, not an integer
‫anymore and we'll get an exception here on handled exception and we even get this Helloworld has stopped
‫working close program.
‫So that's something that you always have to be super careful with and we can handle that with either
‫a try and catch or we can handle it by using tri parse.
‫But those are both things that we are only going to use later on in the course when we will see how
‫to use, try and catch and when we will see how to use if statements because then we can use instead
‫of parse can use, try parse, but it will return an boolean.
‫So we will only find out if parsing worked or if it didn't work.
‫But whatever is with if is decision making and that's something that we're going to cover in a different
‫chapter.
‫But for now, just know that you can convert a string number into an integer or into, for example,
‫a double.
‫So double dot parse.
‫You can see you can do the same thing.
‫So you can parse strings into doubles and into other types as well.
‫Great.
‫So I'd say that's pretty good.
‫In the next video, we are going to have a little challenge and well, please go ahead and try to challenge.
‫And other than that, always be careful when trying to parse.
‫And if you really need parsing and you want to make sure that it really works, then go ahead with the
‫course and you will learn how to handle it.
‫So see you there.