Solved

Format Input Text in Title Case

Posted on 2004-10-18
15
427 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:Ke11ie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
15 Comments
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 12344874
try using split() to first split the string into words.
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12344878
I don't really know how to do that... I'm only a "Learner".
0
 
LVL 92

Expert Comment

by:objects
ID: 12344901
String[] words = s.split(" ");
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 2

Author Comment

by:Ke11ie
ID: 12344920
So then how to get it to only capitalise the first character of each word?
0
 
LVL 92

Expert Comment

by:objects
ID: 12344933
then use your StringBuuffer to convert each word to title case.
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12344959
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
 
LVL 92

Expert Comment

by:objects
ID: 12344977
no there is no simple way, you need to implement it yourself.
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 25 total points
ID: 12345005
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
 
LVL 2

Author Comment

by:Ke11ie
ID: 12345020
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12345038
Ke11ie,

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

\t
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12345813
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12352828
You don't need to iterate over the whole string, only the first character needs to be changed.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question