1 00:00:00,180 --> 00:00:01,020 ‫Welcome, Meg. 2 00:00:01,020 --> 00:00:07,020 ‫In this video, I would like to show you my sample solution for the challenge for switch statements. 3 00:00:07,020 --> 00:00:09,210 ‫And yeah, let's get started. 4 00:00:09,240 --> 00:00:14,520 ‫So first of all, what we all need is a boolean and I'm going to call this one valid. 5 00:00:14,520 --> 00:00:21,750 ‫So this will just take care of knowing whether we are talking about a valid or an invalid data type 6 00:00:21,750 --> 00:00:27,120 ‫and valid or invalid depends on what the user entered after he entered a string. 7 00:00:27,120 --> 00:00:33,450 ‫So first of all, he can enter a string and we will need a variable which will know of the value type 8 00:00:33,450 --> 00:00:37,830 ‫because there are multiple different value types that I want to work with or data types. 9 00:00:37,830 --> 00:00:41,520 ‫And then we are asking the user to enter something. 10 00:00:41,520 --> 00:00:47,670 ‫So I'm just going to say enter a value and we're going to store this in this variable called input value. 11 00:00:48,060 --> 00:00:48,420 ‫All right. 12 00:00:48,420 --> 00:00:55,380 ‫The next thing that I want to ask the user is of which type he thinks the data is. 13 00:00:55,380 --> 00:00:58,950 ‫So select the data type to validate the input you have entered. 14 00:00:58,950 --> 00:01:03,690 ‫So plus one for string, two for Integer and three for Boolean. 15 00:01:04,500 --> 00:01:08,790 ‫And now we of course need to check which of those is correct. 16 00:01:10,100 --> 00:01:17,750 ‫Therefore, we first of all need to get the user input and we need to get the value that the user entered. 17 00:01:17,780 --> 00:01:23,270 ‫So for example, if the user entered one input type, this variable here will be one. 18 00:01:23,300 --> 00:01:25,570 ‫If we enter two, it will be two and so forth. 19 00:01:25,580 --> 00:01:29,750 ‫So we convert whatever the user entered into an integer. 20 00:01:29,780 --> 00:01:33,830 ‫Of course this could go wrong, so maybe you want to use tripods here instead. 21 00:01:33,830 --> 00:01:41,420 ‫But I'm just going to assume that we are only entering one, two or three or in general numbers so our 22 00:01:41,420 --> 00:01:42,590 ‫program doesn't crash. 23 00:01:42,620 --> 00:01:47,030 ‫If you want to make sure that the program really doesn't crash, you could add a try and catch a block 24 00:01:47,030 --> 00:01:49,160 ‫around here and that will definitely help. 25 00:01:49,160 --> 00:01:50,810 ‫Or you could use try pass. 26 00:01:50,840 --> 00:01:51,140 ‫All right. 27 00:01:51,140 --> 00:01:52,290 ‫So that's free to you. 28 00:01:52,310 --> 00:01:54,410 ‫I just kept it simple in this case. 29 00:01:54,410 --> 00:01:57,080 ‫And we are focusing on switch case statements here. 30 00:01:57,080 --> 00:01:57,560 ‫Right. 31 00:01:57,560 --> 00:02:00,740 ‫So let's go ahead and create a switch case. 32 00:02:00,740 --> 00:02:04,160 ‫So what is the switch case going to be interested in? 33 00:02:04,400 --> 00:02:10,490 ‫Well, first of all, what we need is we want to know what the user has input. 34 00:02:10,490 --> 00:02:13,280 ‫So was it one, two or three? 35 00:02:14,100 --> 00:02:19,520 ‫Let's run the code for the case that it is a string. 36 00:02:19,530 --> 00:02:21,840 ‫So for the case the user entered one. 37 00:02:22,230 --> 00:02:28,440 ‫So we're going to create an extra method for this which checks whether everything that the user entered 38 00:02:28,440 --> 00:02:29,460 ‫is alphabetic. 39 00:02:29,460 --> 00:02:30,990 ‫So if it's a string. 40 00:02:32,600 --> 00:02:36,650 ‫Let's look at this method is alphabetic before we go any further. 41 00:02:37,040 --> 00:02:39,500 ‫So we're going to create this a little. 42 00:02:40,510 --> 00:02:44,800 ‫Method outside of our static void main method. 43 00:02:44,800 --> 00:02:46,680 ‫So I'm going to put it over here. 44 00:02:46,690 --> 00:02:53,170 ‫We need to make sure that it is static because we're not creating an object of this class, so we cannot 45 00:02:53,170 --> 00:02:54,130 ‫otherwise use it. 46 00:02:54,130 --> 00:02:56,050 ‫So it needs to be static in this case. 47 00:02:56,050 --> 00:02:59,170 ‫Then we are returning a boolean. 48 00:02:59,170 --> 00:03:03,640 ‫So this method is going to return a bool and we say is all alphabetic. 49 00:03:03,640 --> 00:03:04,960 ‫That's the name of the method. 50 00:03:05,050 --> 00:03:07,660 ‫We need a string, the value that is passed. 51 00:03:07,660 --> 00:03:14,560 ‫And then what we do is for each character in value, for each C, which is a character in our value, 52 00:03:14,560 --> 00:03:15,280 ‫which is a string. 53 00:03:15,280 --> 00:03:16,240 ‫So we can do that. 54 00:03:16,240 --> 00:03:20,230 ‫What we want to check is if the char is a letter. 55 00:03:21,190 --> 00:03:24,040 ‫And if it is not a letter, then we return. 56 00:03:24,040 --> 00:03:24,400 ‫False. 57 00:03:24,400 --> 00:03:30,640 ‫So this whole boolean will be false if it went through the whole string here for this whole value and 58 00:03:30,640 --> 00:03:36,850 ‫each of them is a character that is of type letter, then it will return. 59 00:03:36,850 --> 00:03:37,400 ‫True. 60 00:03:37,420 --> 00:03:43,600 ‫So basically let's say we have a text here which is Hello World. 61 00:03:43,930 --> 00:03:49,930 ‫Then it will go through every single one of those letters here or characters it will check. 62 00:03:49,960 --> 00:03:51,460 ‫Is this a letter. 63 00:03:52,340 --> 00:03:52,900 ‫Then. 64 00:03:52,910 --> 00:03:55,070 ‫Is this a letter? 65 00:03:55,070 --> 00:03:55,850 ‫And so forth. 66 00:03:55,850 --> 00:03:57,890 ‫And each time it's going to see. 67 00:03:58,760 --> 00:04:00,830 ‫This is, in fact, a letter. 68 00:04:01,340 --> 00:04:06,680 ‫And that's why it's going to go through this for each loop without returning false at any point. 69 00:04:06,680 --> 00:04:08,270 ‫And it will return true. 70 00:04:08,900 --> 00:04:18,200 ‫And then let's say we had hello world with a zero here instead and this would be a three because we 71 00:04:18,200 --> 00:04:23,690 ‫are cool and all of that and then that would be returning false. 72 00:04:23,690 --> 00:04:24,530 ‫So it would check. 73 00:04:24,560 --> 00:04:25,820 ‫Is this a letter. 74 00:04:25,820 --> 00:04:26,960 ‫Yes, that's true. 75 00:04:27,080 --> 00:04:28,430 ‫Is this here letter? 76 00:04:28,700 --> 00:04:29,660 ‫No, it's not. 77 00:04:29,660 --> 00:04:31,430 ‫So this whole statement is false. 78 00:04:31,430 --> 00:04:34,880 ‫So it returns out of this whole method and will return. 79 00:04:36,080 --> 00:04:36,680 ‫False. 80 00:04:38,370 --> 00:04:38,670 ‫Okay. 81 00:04:38,670 --> 00:04:40,560 ‫So now we can get back to our case. 82 00:04:40,560 --> 00:04:43,710 ‫One we say valid is alphabetic. 83 00:04:43,710 --> 00:04:46,500 ‫So we pass in the input value, which is our. 84 00:04:47,290 --> 00:04:49,300 ‫String here that the user has entered. 85 00:04:49,300 --> 00:04:51,910 ‫So whatever the user has entered, we check is it alphabetic? 86 00:04:51,910 --> 00:04:56,410 ‫And we say input value type is string. 87 00:04:58,290 --> 00:04:59,940 ‫And then we break out of it. 88 00:05:00,000 --> 00:05:05,970 ‫By the way, this ballot will now be the result, of course, of whatever is all alphabetic return based 89 00:05:05,970 --> 00:05:07,320 ‫on the input that we gave it. 90 00:05:07,740 --> 00:05:13,230 ‫And now let's look at the second case where the user entered an integer. 91 00:05:14,130 --> 00:05:18,360 ‫So here we set the user enter two and it was an integer. 92 00:05:18,360 --> 00:05:21,840 ‫So we check is the return value zero. 93 00:05:22,110 --> 00:05:26,640 ‫And then we check in, try parse input value. 94 00:05:26,670 --> 00:05:33,660 ‫So we pass in our string, we try to parse it, and if it works, then valid will be true. 95 00:05:33,660 --> 00:05:35,730 ‫And if it didn't work, then it will be false. 96 00:05:35,730 --> 00:05:41,150 ‫And whatever was output when it was correct will be in red value. 97 00:05:41,160 --> 00:05:44,170 ‫And then we say the input value type was an integer. 98 00:05:44,190 --> 00:05:45,540 ‫We break out of it. 99 00:05:45,900 --> 00:05:49,470 ‫Now the next case is our case three. 100 00:05:49,980 --> 00:05:53,400 ‫And here we are checking boolean red flag. 101 00:05:53,400 --> 00:05:55,230 ‫And of course you could also use bool here. 102 00:05:55,230 --> 00:05:56,610 ‫You don't need to use boolean. 103 00:05:58,360 --> 00:05:59,560 ‫Red flag. 104 00:06:00,010 --> 00:06:03,130 ‫It's going to be false in this case, the returned flag. 105 00:06:03,130 --> 00:06:08,380 ‫And we use tripods here again, you can see we can use Troy Powers for integers, but we can also use 106 00:06:08,380 --> 00:06:09,370 ‫it for booleans. 107 00:06:09,970 --> 00:06:13,900 ‫And here bool dot try parse with the input value. 108 00:06:14,080 --> 00:06:19,450 ‫If the input value in fact was a boolean, then destroy parse will return true. 109 00:06:19,450 --> 00:06:22,870 ‫Otherwise it will return false and that will be stored invalid. 110 00:06:22,990 --> 00:06:27,010 ‫And whatever the result of this trespass will be, will be stored in red flag. 111 00:06:27,010 --> 00:06:33,250 ‫So if it was a boolean then red flag will be true and our valid will also be true and we set our input 112 00:06:33,250 --> 00:06:34,540 ‫value type two boolean. 113 00:06:34,900 --> 00:06:37,690 ‫And now finally we need to have a default case. 114 00:06:37,690 --> 00:06:41,590 ‫So let's say the user entered anything but one, two and three. 115 00:06:41,920 --> 00:06:46,030 ‫Then we will just say not able to detect the input type. 116 00:06:46,030 --> 00:06:48,760 ‫Something is wrong and we break out of it. 117 00:06:50,130 --> 00:06:55,350 ‫And now we just tell the user if it was validly input. 118 00:06:55,350 --> 00:07:03,180 ‫So if he considered whatever he input correctly and we just say and by the way, this should be one 119 00:07:03,300 --> 00:07:04,440 ‫line further in. 120 00:07:04,500 --> 00:07:12,450 ‫So we just say you have entered a value input value and then we check is the value valid? 121 00:07:12,720 --> 00:07:16,410 ‫And then we say if it was valid console red line, it is valid. 122 00:07:16,410 --> 00:07:20,970 ‫And then we say of which input value type it was. 123 00:07:21,000 --> 00:07:24,510 ‫Now you see it says use of unassigned local variable input value type. 124 00:07:24,510 --> 00:07:30,990 ‫So we are assigning a value here, but we're only assigning it in switch and not in all of its outputs. 125 00:07:30,990 --> 00:07:34,740 ‫So for example, in the default, we don't have an output for our input value type. 126 00:07:34,740 --> 00:07:37,050 ‫So there are two ways of what we could do. 127 00:07:37,080 --> 00:07:44,220 ‫We could say input value type is equal unknown and then the error would disappear. 128 00:07:44,490 --> 00:07:51,300 ‫Alternatively, what we could have done is we could have said that our input value type is string dot 129 00:07:51,300 --> 00:07:52,020 ‫empty. 130 00:07:53,260 --> 00:07:54,580 ‫That would have been an alternative. 131 00:07:54,580 --> 00:08:00,610 ‫And you can see the error also disappears, even though I like this variant more because it's just unknown 132 00:08:00,610 --> 00:08:02,580 ‫and we're going to tell that to the user. 133 00:08:02,590 --> 00:08:08,290 ‫So these are the two options that you had here, but I'm just going to keep it like such. 134 00:08:09,850 --> 00:08:12,160 ‫All right, now let's run it. 135 00:08:14,090 --> 00:08:19,790 ‫And as a value, I'm going to enter 13 and now let's say it is an integer. 136 00:08:20,330 --> 00:08:22,160 ‫You have entered a value 13. 137 00:08:22,160 --> 00:08:23,570 ‫It is a valid integer. 138 00:08:23,600 --> 00:08:25,250 ‫All right, now let's try it again. 139 00:08:25,610 --> 00:08:32,420 ‫This time I'm going to enter Hello World and I'm going to think it's an integer and you have entered 140 00:08:32,420 --> 00:08:35,360 ‫a value Hello world it is an invalid integer. 141 00:08:35,720 --> 00:08:41,930 ‫Okay, now let's check it once again with true and I think it's a boolean and you have entered a value. 142 00:08:41,940 --> 00:08:42,470 ‫True. 143 00:08:42,470 --> 00:08:44,330 ‫It is a valid boolean. 144 00:08:45,190 --> 00:08:45,820 ‫All right. 145 00:08:46,440 --> 00:08:54,630 ‫So now you have this little program which allows you to enter a value and then try to say what kind 146 00:08:54,630 --> 00:08:55,980 ‫of data type it is. 147 00:08:57,700 --> 00:09:04,690 ‫So we have used switch statements, tripods, and we even used if statements as well as. 148 00:09:05,560 --> 00:09:07,180 ‫Hey, for each loop here.