Welcome back.
In this video we're going to look at parsing.
And parsing allows us to convert data from one data type into another data type.
That however, does not work with everything okay.
And we can very easily run into errors using this approach.
So we need to make sure that we enter the right type, otherwise our program will crash.
That is fine for now.
However, in the future in a few hours or so, we're going to cover approaches, how we can fix these
errors entirely, and basically not even run into such a problem.
So how can we now convert whatever we get from readline, which is of type string, as we have covered
before into a data type that we can well into an integer data type that we can store inside of my number.
Well there is this thing called parsing okay.
So we can use int dot parse.
So there is this method called parse inside of int.
So this is a read only struct whatever that means.
We're going to cover that a little later.
But it contains a few methods that allow us to do some cool things with numbers, such as taking something
that is a string and making it a number data type, int data type.
But that only works with numbers obviously.
So we can see here it says converts the string representation of a number to its 32 bit signed integer
equivalent.
Okay.
So I'm going to use this parse method.
And then inside of brackets that's where I put my string.
So the string that I'm going to get is this Console.readline statement.
Because this here is basically the same thing as if I had a string.
Now obviously I can also do it.
Let's say I put this line here, I put it down here and then I use user input because now user input
is of type string as well.
Okay.
So let me put both lines in here so you can see the two lines.
I'm going to however comment this one out so both of them will effectively have the same result.
However here we're saying that we also store whatever the user has entered in a string, and then we
use that string to parse it into a number.
However, that only works if we enter a number.
So let's run this and enter a number five.
And then it says you entered five.
So suddenly it does work.
Our program doesn't crash.
Let me rerun this, but this time I'm actually going to enter a text.
So I'm going to say hi.
And you see our program boom explodes.
We get an error here.
It says exception unhandled system format exception.
The input string hi was not in a correct format, so it could not make a number out of hi.
So you need to enter a full number a integer number.
So if I enter something like 1.5 I get the same error because it tried to convert the string 1.5, which
is again not a number, it is a string, but it tried to convert it into the number 1.5.
But we are only accepting whole numbers, so maybe we should be more precise here where we say enter
a number, we could say enter a whole number.
All right.
Now let's rerun this.
Actually let's rerun this.
Enter a whole number I'm going to enter 15 and it says you entered 15.
So that works.
So now you've seen how you can use parsing in order to take the user input which is of one data type
string and convert it into another data type int.
We're going to see later how we can do that with decimal point numbers as well.
Floating point numbers.
And we're going to use doubles for that.
But that's something for a separate video.
So for this one I'd say I'm quite satisfied.
In the next one we're going to add the addition aspect okay.
So see you there.