‫Welcome, Meg.
‫In this video, I would like to show you my sample solution for the challenge for switch statements.
‫And yeah, let's get started.
‫So first of all, what we all need is a boolean and I'm going to call this one valid.
‫So this will just take care of knowing whether we are talking about a valid or an invalid data type
‫and valid or invalid depends on what the user entered after he entered a string.
‫So first of all, he can enter a string and we will need a variable which will know of the value type
‫because there are multiple different value types that I want to work with or data types.
‫And then we are asking the user to enter something.
‫So I'm just going to say enter a value and we're going to store this in this variable called input value.
‫All right.
‫The next thing that I want to ask the user is of which type he thinks the data is.
‫So select the data type to validate the input you have entered.
‫So plus one for string, two for Integer and three for Boolean.
‫And now we of course need to check which of those is correct.
‫Therefore, we first of all need to get the user input and we need to get the value that the user entered.
‫So for example, if the user entered one input type, this variable here will be one.
‫If we enter two, it will be two and so forth.
‫So we convert whatever the user entered into an integer.
‫Of course this could go wrong, so maybe you want to use tripods here instead.
‫But I'm just going to assume that we are only entering one, two or three or in general numbers so our
‫program doesn't crash.
‫If you want to make sure that the program really doesn't crash, you could add a try and catch a block
‫around here and that will definitely help.
‫Or you could use try pass.
‫All right.
‫So that's free to you.
‫I just kept it simple in this case.
‫And we are focusing on switch case statements here.
‫Right.
‫So let's go ahead and create a switch case.
‫So what is the switch case going to be interested in?
‫Well, first of all, what we need is we want to know what the user has input.
‫So was it one, two or three?
‫Let's run the code for the case that it is a string.
‫So for the case the user entered one.
‫So we're going to create an extra method for this which checks whether everything that the user entered
‫is alphabetic.
‫So if it's a string.
‫Let's look at this method is alphabetic before we go any further.
‫So we're going to create this a little.
‫Method outside of our static void main method.
‫So I'm going to put it over here.
‫We need to make sure that it is static because we're not creating an object of this class, so we cannot
‫otherwise use it.
‫So it needs to be static in this case.
‫Then we are returning a boolean.
‫So this method is going to return a bool and we say is all alphabetic.
‫That's the name of the method.
‫We need a string, the value that is passed.
‫And then what we do is for each character in value, for each C, which is a character in our value,
‫which is a string.
‫So we can do that.
‫What we want to check is if the char is a letter.
‫And if it is not a letter, then we return.
‫False.
‫So this whole boolean will be false if it went through the whole string here for this whole value and
‫each of them is a character that is of type letter, then it will return.
‫True.
‫So basically let's say we have a text here which is Hello World.
‫Then it will go through every single one of those letters here or characters it will check.
‫Is this a letter.
‫Then.
‫Is this a letter?
‫And so forth.
‫And each time it's going to see.
‫This is, in fact, a letter.
‫And that's why it's going to go through this for each loop without returning false at any point.
‫And it will return true.
‫And then let's say we had hello world with a zero here instead and this would be a three because we
‫are cool and all of that and then that would be returning false.
‫So it would check.
‫Is this a letter.
‫Yes, that's true.
‫Is this here letter?
‫No, it's not.
‫So this whole statement is false.
‫So it returns out of this whole method and will return.
‫False.
‫Okay.
‫So now we can get back to our case.
‫One we say valid is alphabetic.
‫So we pass in the input value, which is our.
‫String here that the user has entered.
‫So whatever the user has entered, we check is it alphabetic?
‫And we say input value type is string.
‫And then we break out of it.
‫By the way, this ballot will now be the result, of course, of whatever is all alphabetic return based
‫on the input that we gave it.
‫And now let's look at the second case where the user entered an integer.
‫So here we set the user enter two and it was an integer.
‫So we check is the return value zero.
‫And then we check in, try parse input value.
‫So we pass in our string, we try to parse it, and if it works, then valid will be true.
‫And if it didn't work, then it will be false.
‫And whatever was output when it was correct will be in red value.
‫And then we say the input value type was an integer.
‫We break out of it.
‫Now the next case is our case three.
‫And here we are checking boolean red flag.
‫And of course you could also use bool here.
‫You don't need to use boolean.
‫Red flag.
‫It's going to be false in this case, the returned flag.
‫And we use tripods here again, you can see we can use Troy Powers for integers, but we can also use
‫it for booleans.
‫And here bool dot try parse with the input value.
‫If the input value in fact was a boolean, then destroy parse will return true.
‫Otherwise it will return false and that will be stored invalid.
‫And whatever the result of this trespass will be, will be stored in red flag.
‫So if it was a boolean then red flag will be true and our valid will also be true and we set our input
‫value type two boolean.
‫And now finally we need to have a default case.
‫So let's say the user entered anything but one, two and three.
‫Then we will just say not able to detect the input type.
‫Something is wrong and we break out of it.
‫And now we just tell the user if it was validly input.
‫So if he considered whatever he input correctly and we just say and by the way, this should be one
‫line further in.
‫So we just say you have entered a value input value and then we check is the value valid?
‫And then we say if it was valid console red line, it is valid.
‫And then we say of which input value type it was.
‫Now you see it says use of unassigned local variable input value type.
‫So we are assigning a value here, but we're only assigning it in switch and not in all of its outputs.
‫So for example, in the default, we don't have an output for our input value type.
‫So there are two ways of what we could do.
‫We could say input value type is equal unknown and then the error would disappear.
‫Alternatively, what we could have done is we could have said that our input value type is string dot
‫empty.
‫That would have been an alternative.
‫And you can see the error also disappears, even though I like this variant more because it's just unknown
‫and we're going to tell that to the user.
‫So these are the two options that you had here, but I'm just going to keep it like such.
‫All right, now let's run it.
‫And as a value, I'm going to enter 13 and now let's say it is an integer.
‫You have entered a value 13.
‫It is a valid integer.
‫All right, now let's try it again.
‫This time I'm going to enter Hello World and I'm going to think it's an integer and you have entered
‫a value Hello world it is an invalid integer.
‫Okay, now let's check it once again with true and I think it's a boolean and you have entered a value.
‫True.
‫It is a valid boolean.
‫All right.
‫So now you have this little program which allows you to enter a value and then try to say what kind
‫of data type it is.
‫So we have used switch statements, tripods, and we even used if statements as well as.
‫Hey, for each loop here.