This course delves into the String class and looks at the functionality it provides through a host of methods. We will also discuss how string objects are immutable, which means the object itself can't be modified once they've been created.
Learning Objectives
- Learn how the StringBuilder class differs from the string class
Intended Audience
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
Prerequisites
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In this section, we've learned a lot about how to deal with textual data. Our primary class for working with text data is string. In this project, you will create a file called Proj5_1_NamePermutations in camel case. Inside the class, you will write your regular main method and you will ask the user to enter five full names. First name, the given name, followed by a space, and then the last name or family name. As you take each full name in as input, you will use the appropriate string methods to separate the first and last names, placing the first names into an ArrayList of first names, and the last names into another ArrayList of last names.
Once you have obtained all five full names, you will iterate over the ArrayLists in order to print out all possible permutations of the first and last names. In other words, for each first name, you'll print the first name with each of the last names. Since there are five first names and five last names, there should be 25 total permutations. All right, let's give this project a run without using the code. So, you can see kind of what it's supposed to look like. So, we'll run the project right here, and it will have us enter names. Name 0, name 1, name 2, name 3, name 4. So, I'll put John Baugh, Keegan DeLancei, George Johnson, Bob Brandanstein, and Ian Leath. All right.
So, it takes each of the first names and it tries them with each of the last names just to come up with different names. So, it goes the second first name and then prints out all the last names with that. And then the first name and goes with all those etc, etc. Hope that helps. So, pause the video and give this project your best attempt. Think it out carefully before you start coding. How did that project go for you? This one was a bit challenging because it combined what you just learned about strings in this section with what you learned about ArrayLists previously. Let's work on it together. So, let's create the Proj5_1_NamePermutations file if not done so already. So, right click that 'Java Class' and we have Proj5_1_NamePermutations. All right, hit 'Enter' a couple times and then we have public static void main and that's the end of main. All right.
We'll need the ArrayList java.util.ArrayList, and also java.util.Scanner. Here we go. So, an ArrayList of strings called firstNames. There will be new ArrayList, and then an ArrayList of strings called lastNames. All right, so far so good. We need a scanner with System.in passed in, and we're going to create some variables to hold some of the data as we go along. So, String fullName to grab the entire name, firstName, we also need one for the lastName, and notice I'm putting no s on the end here. These aren't plural. That would be the ArrayLists right there. And then, I'm going to have an integer called indexOfSpace, because I'm going to grab the index to know where the space is in order to split the names. final_int NUM_NAMES is 5. All right? And now for(int i = 0; i < NUM_NAMES; i++), since there's a static number of names. A specific number.
I'm going to say system.out.print this time, I'll say "Please enter name" and then I am going to put i, and it will print out 0, 1, 2, 3, 4 and a tab following it. fullName = keyboard.nextLine(); So, that's the full name. We've grabbed the entire name. Now, we have to split it. So, to do that what we're going to do is we're going to find indexOfSpace using what we know so far indexOf, then we are going to actually look for a space and firstName = fullName, substring starting at the beginning and going up to but not including the ending index, in this case the indexOfSpace. So, no matter what their first name is, it will go from the beginning up to including the character right before the space. And that's exactly what we want. For lastName, we want fullName.substring, we want to start at the indexOfSpace plus one because we're starting after where the space occurs and we don't need to specify a second argument because it will go to end automatically if we don't. And now, we have the first name and last name for each iteration, but we have to store them in order to keep them otherwise they'll keep getting overwritten.
So, firstNames.add(firstName) ; Be very careful with this because they are similar variable identifiers and the same thing with lastNames.add(lastName); All right? So far so good. Now, for the permutations. So, we're grabbing all the stuff in our firstNames ArrayList. So, we're going up to firstNames.size() and for you could also use the range based or enhanced for loop as well if you wanted to. All right, let's see what we got here. Seems to be angry about those for some reason, variable i is already defined in scope. This is in the loop. We don't need that. See, it's good to look out for errors. So that, this whole part needs to go outside this for loop. So, sorry about that. So, you can just cut and paste it or re-type it.
So, now for the permutations and then for(int i = 0; I can reuse i because I'm outside of the scope of this loop but I had accidentally put it inside the loop. So, don't put it inside the loop, put it outside the loop. All right. So, I think I'll leave that mistake in instead of making it perfect because you can see that all of us make mistakes sometimes. You just have to figure out what the mistake is and that's important. Okay, so for i and for j. So, we're going to do System.out.print(), just print, firstNames.get(i) because that's what iteration we are on that, then a space and then what we're going to do firstNames, sorry, firstNames, System.out.println() because now we can end it (lastNames.get(j)); and then you have the end of for i and end of for j and of course, we want to run it and test this out.
So, let's right click and run the project. All right. Please enter name 0, I'll put John Baugh, we'll put Sally Robinson, Bill Williamson, Silas Gray, and the Ksenia Malmstein. So, now you'll notice that it takes each of the first names, and it tries them out with each of the last names. And that's what I meant by permutation, of course. Sally Robinson, Sally Baugh, Sally Gray, etc, etc, and that's pretty cool. So, it takes each of those first names and then tries it with each of the last names. Very nice.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.