‫We'll be fixing to get our search for a city method done and did today.
‫So let's get started.
‫Here we have our handy dandy array list of cities.
‫We need to ask our user to enter a city name.
‫buddie he says Azusa.
‫So we store this input in a string object and we create a boolean called Exist and we set it to false since
‫we have no idea if it exists yet.
‫Now we go on the hunt.
‫C1? that ain't Azusa so skip C2 nope C3 no C4 we found it exists is no longer false We
‫found Azusa so we set it to true Print the contents of C4 and break out of our loop so that we
‫don't keep looking.
‫If, however, we did not find the city, we keep looking.
‫C five no.
‫C six no.
‫C seven.
‫C8, C9.
‫Still can't find it.
‫Our boolean exists was never set to true.
‫Therefore we know that we found nothing.
‫We let the user know Hey, we didn't find city. before we go full conquistador, let's dice and slice
‫a few things.
‫One upper and lowercase characters matter in Java.
‫If we compare these two strings, they are not equal.
‫So we can use the method called to lowercase and compare them.
‫Well, what the hell?
‫It's still not equal to.
‫Why?
‫Well, because strings are objects.
‫And when we use equal equal to on objects, we compare memory, location and not contents.
‫So we need to use to lowercase dot content equals to in order to compare these two strings and get
‫the result we need.
‫Okay, that's all there is to it.
‫Let's get it done.
‫pause the video here and try it solo first.
‫Welcome back, you good looking m****.
‫First, let's create our search first city method.
‫So public static void search for city no parameters.
‫Open close bracket.
‫And then we say P enter city name.
‫Now let's create a string.
‫We call it cn for a city name and we set it is equal to SK Dot next to lowercase.
‫So we get user input and before storing it in CN we make sure it's lowercase since uppercase city and
‫lowercase city are not equal to each other.
‫Now we create a boolean and we call it exists and we set it to false to ensure city was found.
‫And now we get to our loop.
‫We want to loop through the entire array list so int I is equal to zero.
‫I less than cities size.
‫I-plus plus.
‫With each loop.
‫We want to check the element at this position of the loop.
‫So we say if cities get I.
‫Get name.
‫. two lowercase.
‫. content equals and we pass the cn which holds user input and we use content equals so that we
‫aren't comparing memory location but rather the contents of the string.
‫And if we find what we are looking for, we set exists is equal to true and print.
‫What we found. our two string is built for display.
‫So we'll just quickly write a print statement.
‫So we'll say system .out . print line city.
‫Plus cities get name.
‫Plus systems line separator.
‫And then we say plus latitude.
‫Plus cities get lat.
‫Plus system line separator.
‫Then we say plus longitude.
‫Plus cities.
‫I . get lng.
‫Plus system line separator.
‫And finally, plus population.
‫Plus citizen get.
‫I get pop.
‫Plus system line separator.
‫Outside of the loop.
‫Let's just make sure we actually found the city we're looking for.
‫So we say if exists is equal to false.
‫And we print city does not exist.
‫And skip a line by saying system line separator.
‫Now we have a bit of an oversight in our code.
‫Let's say we find our city in element three and our array is 300 elements long.
‫We still traverse the entire array after we already found our city.
‫So let's throw a break in here to give us an exit.
‫Now let's go over to our main menu method.
‫And instead of this useless output, we'll run.
‫Search for city.
‫If we write a, we get our city.
‫If we print gibberish, we get nothing.
‫Weird.
‫Let's check what we did wrong.
‫Ah if exists is equal to false instead of if exists is equal to equal to false ok.
‫Now.
‫We're good to go.
‫But let's skip two lines instead of one.
‫So just copy paste that.
‫And nice.
‫Looks good.
‫Everything works.
‫We are done with search for a city method.
‫My personal opinion that you probably should stick around for the comments and write comments that make
‫sense to you.
‫Otherwise this won't make sense in a few weeks.
‫But if you want to skip that part and just go straight to the next video.
‫You do, you Booboo.
‫I'll see you guys there.