 Hello, time for the mini lecture. First topic I want to talk about is one that I I skipped for a couple of days now, and this time I wrote down what I want to talk about. And the first thing is nested loops, namely loops inside of loops. So here we have a loop that's a for loop that goes counting x from 1 to 10. And inside of it is one that has y from 1 to 10. Inside that loop, we multiply x times y and then print it out. And this goes all the way through and then we print a new line. Then we return to the outer loop where x becomes 2 and then we start again with the inner loop. You know what I think we need to do here? I think we need to go to the Java visualizer. So let's go to Java visualizer here and let's pop this in here and let's get rid of the scanner because we don't need it. And let's see what this is actually doing. So here we go. x starts as 1. 1 is less than or equal to 10. So we now go into our inner loop where y becomes 1. 1 is less than 10. So we print out 1 times 1, which is 1 here. And then we'll go forward. Now y is 2. 2 is less than or equal to 10. We print 1 times 2, which is 2. And I'm going to keep going through this here. y is 4, 5, 6, 7, 8, 9. And 9 is less than or equal to 10. Now y becomes 10. 10 is less than or equal to 10. Therefore, we do the multiplication 1 times 10 and print that out, which prints out a 10. Now y becomes an 11. Remember, even though we know the loop is done, Java still does the action. So now y is an 11. And 11 is not less than or equal to 10, which dumps us out of the inner loop. And now we go to the next line. Now we come back up here. x was 1, and now we do the action. And x becomes a 2. Now we go into the inner loop again. We start y back at 1. 1 is less than or equal to 10, which means we are now going to do 2 times 1 and print that out, which gives us a 2. y moves up to 2. 2 is less than or equal to 10. 2 times 2 is 4, and we print that out. y is 3. 3 is less than or equal to 10. 2 times 3 is 6 and so on. And let's move that up again here. So y becomes 9. 9 is less than or equal to 10. We print 2 times 9. 10 is less than or equal to 10. We print 2 times 10. y moves up to 11. 11 is not less than 10, which means we go out of that loop, print the new line to go to our next line. And now we are back to our top loop where x is 2, and it moves up to 3. 3 is less than or equal to 10, and we go through this again. So that's how a nested loop works. And in fact, if we were to compile this and run it, you would see we get the multiplication tables from 1 to 10. Yeah, I'm going to do something here. So let's try this. Say this as triangle of stars dot Java. So we want to do here and let's me rename it right away. So enter in an integer. And print that many lines of stars to make a triangular pattern. For example, if the number is 4, we print 1 star on the first line, 2 stars on the second line, 3 stars on the third line, and 4 stars on the fourth line. Now, one of the things that they did in this program is they used x and y as their loop variables. And you'll often see people using the, the variable names i and j for the inner and outer and inner loops. I like to have a little bit more meaningful variables so that the code becomes clear. So first of all, we need to enter a number of lines. And we're going to have integer and lines becomes input dot next int. So if the number of lines is, well, we don't even need an if statement there. Now what we're going to do is for int line is zero line less than the number of lines line plus plus. So our outer loop is going to draw one line at a time. Our inner loop is going to draw stars will set stars is zero. Stars is less than the line number. So on line zero, we're going to print one star zero and then zero one zero one two and zero one two three. I think I'm going to need less than or equal to line. Let's check here when I have a line zero, stars started zero and I want less than or equal. Otherwise I won't drive. I'll be one short. And then stars plus plus all print one star. When I'm done printing the stars for that line, I do a system dot print line. And let's see, let's compile that. And let's see if that works properly. So let's say four lines. And sure enough, there's my four lines of stars, my triangle of stars. If I say I want 15 lines. I get that pattern. So that's another use of nested loops. Admittedly it's not a useful one, but it's an interesting one. And again, notice that I am, by the way, what would have happened if I had said stars less than line? Let's think what would have happened. Line would have been zero stars would have been zero. Is zero less than zero? No, it is not, which means we would have never gotten that first star on the line. And then we would have gone to the next line. And in fact, that's exactly what would happen if I said I want four lines. I'd be one star short on every line. Now I could have also said stars less than line plus one, or I could have said stars less than or equal to line because zero is less than or equal to zero. I would have printed the one star. Stars would have become one, but one is not less than or equal to zero. And I would have dropped out and there now it's fixed again. Yeah. So that's nested loops onto strings. And I think I'm going to use J shell for this. So remember, I have a string S here. And let's see. Well, let's do something here. ABC. And we had methods like length. And that would give us back the length of the string. Now there are a lot of other things that we can do. One of them, let's set S to something different. I'm going to put four blanks. A tab. And then I'm going to put a couple more blanks ABC. And then I'll put a couple more tabs. And a few more blanks there. And now there's a method called trim. I can say. Spring. No. Leading or trailing. Well, I'm sorry. No leading. Trailing blanks is S dot trim. And what that will do is it'll trim off any leading white space, blanks, new lines and tabs. And I get just the ABC. Now notice that I had multiple blanks in between ABC and D E F. Trim does not trim them down to a single blank. It leaves those alone. It only gets rid of the initial blanks at the beginning and then. Ending blanks. And those get cut away. I can have shout becomes S dot to uppercase. If I have a string called. Well, let's call it a message. I can say string whisper is message to dot to lower case. So that to lower case takes everything that's a letter and reduces it to lower case. And this is sort of handy to have. Now to very, very important. Methods that strings can do are the index of. And the substring method. I forgot to make my notes file. Keep forgetting to do that. So let's say I have a string ABC comma space D E F. And I'm going to number the indexes 01234567. And let's say that this is in a string called str. If I say str dot index of. comma. That will tell me where the first comma is in this string. It'll give me back a three. In fact, let's go back here and put it in GH. 8, 9, 011 and 12. If I say string dot last index of. comma. That will give me the eight. If I have the string banana. And I say. String dot index of a and you can go for more than one letter. That will give me back a one. Let's say string dot index last index of. And that will give me back a three. Let's test this to make sure that's correct. Can I turn on a terminal here? No, I can't. Okay. So let's go here and say string. It was ABC D E F GHI. And I say string dot index of comma. That will give me a three. Last index of comma. Gives me an eight. And then let's say string fruit becomes a banana. And I say fruit dot index of and. Gives me a one. Fruit dot index. Last index of excuse me. Gives me a three. Just as I predicted. Now, what if I try something like string dot index of. D F D. That's not in the string anywhere. And so what it gives me back is a negative one. What if I say fruit dot index of letter B is going to give me a zero. So negative one means not found. There's also the substring method, which is again a very important one. So let's have a string here and I'm going to. Set to ABC D E F GHI J K L. And then I'm going to number these so we can see zero one, two, three, four, five, six, seven, eight, nine, zero one. Okay, that's 10 and 11. If I say string dot substring, the generic form is I give it the from and to positions. And that gives me the portion of the string. Starting at the from position. Up to, but not including. The two position, you know what, instead of from and to, which is a bad name. Let's call it start and end. Well, it starts at the start position. So let's try this and see in J shell what that gives us. So if I say string. Is going to be a maybe CDF GHI J K L. And I say string dot substring. From let's say two to seven. And let's look at this here so we can see this at the same time. So it's just give us starting at C up to, but not including. H. So it should give us CDF G. And let's try that. And sure enough, it gives a CDF G. If I say stirred out substring from zero to two. That'll give us the first two letters, which are a B. Now there's another flavor of substring. If I say stirred out substring seven goes from seven to the end of the string. It's exactly the same by the way as saying sure. Substring of seven to. Stir dot length. Up to, but not including the length of the string. That would give us the same thing. But it's sometimes so common to have this last thing here. That this shortcut is very handy. Something very important to note. Index of returns a number. Not a string. Substring returns a string. All in this is really important. All of the string methods. Leave the original string untouched. For example, let's look at str. If I say str dot to uppercase. That gives me a brand new string with everything translated uppercase. If I look at what's in str. It hasn't changed, which means when I use things like to uppercase or to lowercase or substring or trim, I have to assign it to a new variable. I have to say string upper string. It becomes. Stir dot to uppercase. Now I have upper string. And str, and they are separate from each other. So all of the string methods always, always, always give you some new string or some number. And they always leave the original string untouched. There's a fancy phrase for that. Okay. Strings are. Immutable. All the method calls that you do on a string will leave the original string exactly the way you had it before. Okay, now let's use. Index seven substring to do something sort of interesting here. So we're going to go here and let's open up our template file. Save this as city state zip dot Java. The program is we're going to ask the user. To enter a string in the form. City comma state. And zip code. And then split it up into its constituent parts. In fact, let's repeatedly. Ask the user to enter the string in the form. And we'll call this city state zip. So here we have string address. And we'll set to the empty string and we'll have a do while loop. And then we'll have address becomes input dot next line. I said next, which would definitely not have worked. And then. If the address. Is not equal to the empty string. Then we'll process it. And we'll do that as long as the address is not equal to. The empty string. You know what we might want to do here by the way. Let's do address becomes address dot trim. We can reassign a variable. In fact, let's go back here really quickly. I could have said, for example, string becomes string dot to uppercase. And that will change string because now I'm reassigning it. But the two uppercase itself doesn't do anything. It's the reassignment that changed it. So in case the user presses a space bar before they enter their stuff, I wanted to trim everything off. And let's just check to make sure that this works here. Let's say. Great. I don't know, of course, if I say blah, that's going to not valid. And we're going to have to worry about that a bit later. And let's just print. Enter to quit. Now the question is, how do we split this up? Here's the trick that we're going to use. Let's plan this out a little bit. Consider this. I have city name. Comma. And then my two letter abbreviation. And then 99999. So what I really want to do is I want to find the first comma. The next thing I want to do is I want to find the last space. That means everything before the comma. Is the city. Everything between the comma. And the last blank. Is the state name. And everything after the last space is the zip code. I can use index of and last index of to make my. Life rather interesting here. So let's go and look at this. So we're going to have here an integer. Comma position is going to be address. Index of comma. We're going to have a last blank. Position is going to be address. And the last blank position is greater than or equal to zero. And the last blank position. Is greater than or equal to zero. That means we at least have a comma and a blank. Otherwise. We can say. I don't recognize that address format. So we have to have both a comma position and a last blank in there. And we probably also want the last blank. To be after the comma. Because if the last blank is before the comma. That would mean things are getting pretty weird. We'd have the last blank here. If I left off the zip code. Yeah, everything. Everything would come out pretty weird. So I need one more condition. And. The last blank position. Has to be greater than the comma position. Notice, by the way, I've split this on the two separate lines, because if I put it all on one line. That line would have gotten awfully long. So we'll indent it just a little bit. Now I can split out my parts. I can say. String. City. Is going to be. Address dot substring. Starting at zero up to, but not including the comma. My state. Is going to be the address. Substring. Starting at the comma position plus one, because I have to move past the comma. And I'm going to end. At. Last blank position up to, but not including that. And then finally. Okay. My indenting is a little bit off here. And I'm not sure why. Oh, it's because I did this crazy thing here. Tell you about, let's do this. Let's move everything so that it aligns properly. Yeah. And then my zip code is going to be added. And I'm going to. Address dot substring. From last blank. Position plus one. To the end of the string. Now. The problem is what happens if somebody types in something weird. Oops. Like, for example, Cupertino. California. Comma 95014. I want to get rid of all those extra blanks. There's no blank. Sorry. There's no comma there. Okay. All right. So I'm going to say city becomes city. Trim. State becomes state. Trim. And zip becomes zip. Trim. Then I can print them out. And let's compile that. And let's see what we've got here. So if I have Cupertino, California, 95014. That worked pretty well. How about, let's say. Carson city. Nevada. And I have no idea what the zip code is. I'm just going to make something up. In fact, oh, let's put in some extra blanks here. Oops. Can I use the back arrow? No, I can't use my back arrow here. Okay. Well, let's try this. So let's put some spaces in here. And there we go. And yeah, I know, I know, I know. Let's put a blank line in there for readability. There. I feel happier now. Well, actually, we can say Santa's. So that's pretty neat. The fact that we can use. Index of. Last index of. And substring. To take a user's string and split it apart into. Units that we can manipulate later on. Individually. That I believe finishes up what I want to talk about with chapter six. So I have no idea. I don't know. I don't know. I believe finishes up what I want to talk about with chapter six. So I have no idea what's going to happen tomorrow in terms of the mini lecture, but I'm sure I will think of something. See you all then.