Java Fundamentals Jumbled String
Transcript from the "Jumbled String" Lesson
>> We're gonna write a method that adds spaces to a jumble string where all the words were written together with no spaces, and then a little fact each new word begins with a capital letter. So let's make a new one. And we'll call this one addSpaces. And then we wanna add a string.
[00:00:23] I don't know, something, HeyWorld! It'sMeAngie. Be careful, the instructions say that each new word started with a capital letter, so that is our indicator on how we find the new words, okay? So we'll add our new method I'm gonna use my good buddy var, and we'll say modifiedText=, and now I'm gonna call into the string builder class, new StringBuilder.
[00:01:15] And I can give the StringBuilder the text. So this is what calling it to the constructor, StringBuilder that takes a string, and it's basically gonnareturn me back. What are you returning me? A StringBuilder object. Okay, And with that StringBuilder now I can do some fun things. So let's go ahead and we're gonna loop.
[00:01:47] I wanna use the regular loop here. So I wanna say i Is less than the length of the modifiedText. And I can say less than, cuz that makes sure it doesn't go all the way to the end. And then i++. And then inside of this loop I want to check that specific character.
[00:02:18] If it is, Uppercase, I want to what? You wanna addSpaces, so if it's uppercase, I would wanna add a space before it, right? Okay, except for the first one, I don't wanna space right here. Does that that logic makes sense to everyone? All right, so let's add a loop, a if statement.
[00:02:47] So if i does not equal 0, it's not the very first character, right? And, I'm going to use a wrapper class now, character
>> Do the double equal sign?
>> I do, where? Not equal.
>> Nevermind, sorry.
>> [LAUGH] I'm glad that you're thinking that way, though. All right, so this character wrapper class has, again, a whole bunch of convenience methods that are static and readily available.
[00:03:27] isAlphabetic, isDigits, so you can see where this could become very helpful for you. One of them is is uppercase. Yeah, because it is static, it has no context. So I have to give it the character. So I wanna see var modifiedTexr.charAt(i), okay? So if it is not the first character and if the character is an uppercase character, what I wanna do is insert a space before that.
[00:04:13] So I can say modifiedText.insert. And I give it the index that I wanna insert before, and then I give it what do I want inserted here. So I want a white space inserted there, okay? And, At that point, well, another point. So I'm inside of this for loop, right?
[00:04:46] And this is going through each character. What I've just done is, I've just inserted a space before it. So I'm kind of messing my numbers up, right? So what I wanna do is advance i by 1. Sorry, to put it back where it was before. Okay, so that we don't have to keep going down the same path.
[00:05:17] All right, and then after the for loop, let's just print it out our new string modifiedText. All right, let's run it. Look at that. Any questions on StringBuilder? Yes.
>> What's the relevance of passing the text into the StringBuilder class?
>> Yeah, this constructor here. So right here, StringBuilder, in order to have a string that's a part of this object's state, you would want to give it the initial string that you want it to deal with.
[00:06:19] Let's see if it even has a default constructor. It does have a default constructor. So in that case, if I wanted to do something else, still it has no concept of what the text actually is. So I could do anything I want it, but I'm gonna end up with some sort of exception when it tries to run.
[00:06:41] It's gonna say, say, I don't have a text for you, I don't know what you're talking about. Or maybe the constructor sets it to, call super. Yeah, it's just basically setting some mumbo jumbo stuff. But the reason why we sent it the text is so that it has some state to initially deal with.