Format Input Text in Title Case

The user is promted to input a name. I want to make sure that the formatting is correct - in Title Case.

For example, if the user types in, bob smith, all in lowercase I want java to format it correctly into: Bob Smith - formatted in Title Case.

I am trying to work something out by using StringBuffer...

I may eventually get there, but to speed up the process, you may know something really simple!
LVL 2
Ke11ieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
try using split() to first split the string into words.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ke11ieAuthor Commented:
I don't really know how to do that... I'm only a "Learner".
0
Mick BarryJava DeveloperCommented:
String[] words = s.split(" ");
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Ke11ieAuthor Commented:
So then how to get it to only capitalise the first character of each word?
0
Mick BarryJava DeveloperCommented:
then use your StringBuuffer to convert each word to title case.
0
Ke11ieAuthor Commented:
I can only convert all words to uppercase or all words to lowercase. Is there a simple way for Title Case? I don't know how to do this.
0
Mick BarryJava DeveloperCommented:
no there is no simple way, you need to implement it yourself.
0
Tommy BraasCommented:
Hi Ke11ie,

Instead of splitting the String up into the parts, you could create a StringBuffer with your String, and iterate over it replacing the first character after a white space with its upper case character.
A simple for-loop will do. Don't forget the first character!

\t
0
Ke11ieAuthor Commented:
The for loop does sound like a good idea but i still don't really know how to make it change each of the characters. Do I use something like CharAt(" ") + 1? toUppercase?
0
Tommy BraasCommented:
Ke11ie,

There is a static toUpperCase() method on the Character class and a setCharAt() method on the StringBuffer class. Use those methods!

\t
0
zzynxSr. Software engineerCommented:
>>The for loop does sound like a good idea but i still don't really know how to make it change each of the characters.
>>Do I use something like CharAt(" ") + 1? toUppercase?

1) You have your string you want to title case
2) Have a StringBuffer too (initially empty)
3) Iterate over your string
    with yourString.charAt(i) you can get the character at index i
    with yourString.charAt(i-1) you can get the character at index i-1
    a) if it is the first character of the string : append it its uppercase version to the StringBuffer
         [ Character.toUpperCase(theOriginalCharacter) returns the upper cased character ]
         [ You can append a character by just calling append(yourCharacter) ]
    b) if the previous character (the one at index i-1) is a blank :
        append the uppercase version of the character at index i to the StringBuffer
    c) if it is none of the above cases a) or b) just append the character at index i to the StringBuffer
4) After having iterated through the whole string this way, your StringBuffer contains the title cased version
    of your original string.
0
Mick BarryJava DeveloperCommented:
You don't need to iterate over the whole string, only the first character needs to be changed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.