 Hi guys, so today we're going to be taking a look at how to solve array validation problems in Java So what exactly is a validation problem? A validation problem is one in which we're given a certain criteria We want to search through the array and then if we find any value in the array that meets the given criteria We want to return true if we don't find any items in the array that meet the given criteria Then we're going to return false So you can see for this example We're going to be working on this method contains even value So you can see here at this method just contains one parameter and integer array data And we're going to return true if any of the items in data are even And then we're going to return false otherwise Now a good way of approaching a problem if perhaps you're not sure where to start with writing the code Is to go ahead and start by writing some pseudo code and kind of going through your thought process of how you're going to solve the problem So we can go ahead and do that just up here Okay, so we're going to obviously need to search through the array and You know start looking at the items in the array to determine if they're even So the first thing we're going to do is search Through each item in the array Now if we find a current item that's even What does that mean? Well, if we find any item in the array that's even then obviously we know that the array does contain an even value Which means that we don't need to check any more of the items in the array. We can just go ahead and return true So return true If the current item is not even we're just going to keep searching through each item consecutively in the array Eventually we will reach the end condition of our loop We'll end the loop now if we get to the end of the loop and we have Been yet to return true That means that we've checked every single item in the array and none of the items have come up even Which means we know none of the items in the array are even and we can just go ahead and return false So that's it. That's how the Code is going to look so now let's go ahead and try writing that in actual Java so We're obviously we're going to need to use some sort of loop to search through the array, so I'll go ahead and use a for loop so we'll say for Into I equals zero. I is less than data dot length I plus plus if Data I Modulo Where is my there it is modular to equals zero So that's just checking for If the item is even then data. I modular to will equals zero Then we know the item is even we're going to return true can't type and we're going to keep Looping through data checking each item Eventually we're going to get to the end of our loop And then if we get to the end of our loop and we find that None of the items in the array have been true I mean have been even sorry then we're going to go ahead and return false So now that we've written the method we can look down here and see that I've written some j unit tests In order to test the contains even value method So let's go ahead and run this now as a j unit test and we'll see if the method that I've written passes all the tests Okay, so when I run it right away, you can see I've got an error And if we look down here, you can see that I've experienced a null pointer exception and then if I look Here you can see that it's occurred online 39. I'll just double click. Whoops double click that So it's occurred at the first test a cert false contains even value null So this is something that's going to happen really often when you write a method you test it It's going to fail on one of the tests maybe because you've failed to account for say a Null array or an empty array So this is what's happened here. I've failed to account for the array Well, I've failed to account for my method being passed a null value rather than an array So Often as well, it's worth pointing out. It often won't be intuitive how necessarily you're supposed to deal with say an empty array or a null array like in this case it might not be intuitive whether you should be returning false or True for a null array, but you can always take a look down in the test and that will You know that should if the tests have been written correctly tell you how you're meant to be dealing with these values So now we can see that when we're When our method is passed a null array then we want to be returning false so I can go ahead and Now deal with that accordingly by just adding Something to deal with null values, so we can go ahead and say for well not for if data equals null Then we want to return false Now I'll go ahead and Run this again, and we can see now that my method has passed all of the tests It's also really useful as well, so it's not just for null and empty arrays Sometimes for example if you're dealing with odd and even values You might not You might not intuitively know whether or not for example You're supposed to consider zero and even value for the purpose of the problem that you're trying to solve But it's really handy to remember you can always just look down in the test and that should tell you Generally speaking what you're meant to be doing with edge cases such as that so if I look down here I can see that If I have an array that just contains zero Then I want my method to return true and therefore I can see okay zero is obviously considered an even number For the purpose of this method that I'm writing So that's just a quick look at how to deal with validation problems in arrays So these can take many forms. This is just one example, but the General rule with validation problems is that you're always Searching through an array for any value that meets the given criteria. It could be that you're looking for Any even number any odd number Any positive number any negative number, but the general format of How you solve the problem is generally always going to be the same the only thing part of this really that we're going to be changing is Really a lot of the times just this if statement right here So for example, let's say instead of this method being called contains even value We have a method contains positive value So now instead of searching for any even value in our array We're searching for any positive value The only part of this that we're going to need to change is this part right here where we're going to instead be Saying if data i is greater than zero Then we know we've found a positive value We can return true if we get to the end of our array and we haven't returned true. We return false exactly the same So yeah, I hope you guys Have a better understanding now of how to deal with validation problems with arrays I would also definitely recommend that you take a look at our video on Violation problems, I'll put a link to that down in the description below Because I think that oftentimes people get confused About the difference between validation and violation problems and the different approaches that you need to take in dealing with each of those types of problems So yeah, I hope you guys found this helpful. Thank you for watching and have a great day. Bye