Solved

Problem converting String to Title Case

Posted on 2003-11-26
11
359 Views
Last Modified: 2011-09-20
Hello people,

Please could someone slove this for me. I have a method that is passed a string from a JTextfield on my GUI. Now I want to take that string and convert the first char and any char after whitespace into Capitals.

So for example  'springfield house' would become 'Springfield House'.

Below is what I have but the compiler does not want like it one bit!!!!!

public void setAddress(String newAddress){
       
for (int count = 1; count < newAddress.length(); count++){

             if (newAddress.charAt(x).equals(" "))              //If char equal a blank space
                   newAddress.ChartAt(x++).toUpperCase();
             else
                   count++; }
        address = newAddress;}                //The variable ;address ;(String)

Can anyone help. The more simple the answer the better!

Shame String has'nt got a 'toTitleCase' method!

Thanks

Beam
0
Comment
Question by:Beam
  • 6
  • 3
  • 2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827931
Quick and dirty and untested

  public static String toTitleCase(String s) {
    StringBuffer sb = new StringBuffer(s.trim());
    for(int i = 0;i < sb.length();i++) {
      char c = sb.charAt(i);
      if (c == ' ')
        sb.setCharAt(i + 1, Character.toUpperCase(sb.charAt(i + 1)));
    }
    return sb.toString();
  }


You might want to move the first char to upper case come what may...
0
 
LVL 92

Expert Comment

by:objects
ID: 9827945
try:

StringBuffer temp = new StringBuffer(newAddress);
for (int count = 0; count < temp.length(); count++)
{
   char c = temp.chatAt(count);
   if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c))
   {
        temp.setCharAt(Character.toUpperCase(c));
   }
}
address = temp.toString();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9828026
>>You might want to move the first char to upper case come what may...

If that is the case (not sure what the 'rules' are) then do

sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));

before returning
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9828101
> not sure what the 'rules' are

it's specified in the question :)

public void setAddress(String newAddress)
{        
  StringBuffer temp = new StringBuffer(newAddress);
  for (int count = 0; count < temp.length(); count++)
  {
     char c = temp.charAt(count);
     if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c))
     {
          temp.setCharAt(count, Character.toUpperCase(c));
     }
  }
  address = temp.toString();
}
0
 

Author Comment

by:Beam
ID: 9828253
Objects, I tried your version first but it throws the  syntax error ' StringBuffer can not be applied to Char'  for the line  'temp.setCharAt(Character.toUpperCase(c));' ????

About to try yours CEHJ.
0
 
LVL 92

Expert Comment

by:objects
ID: 9828269
> but it throws the  syntax error

That was a typo, it's fixed in the 2nd code I posted.
0
 

Author Comment

by:Beam
ID: 9828286
Sorry Objects just got ya last version and it works great.


Thanks for your time guys.
0
 
LVL 92

Expert Comment

by:objects
ID: 9828297
I think CEHJ's will crash if you pass it a blank string, also wasn't sure if you wanted the leading and trailing spaces trimmed.
Change last line to:

address = temp.toString().trim();

if you do.
0
 
LVL 92

Expert Comment

by:objects
ID: 9828301
Happy we could help :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9828474
>>I think CEHJ's will crash if you pass it a blank string

And yours will crash if you pass it a null String ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9828503
good point:

public void setAddress(String newAddress)
{
  if (newAddress==null)
  {
      address = null;  //or u might want to set it to blank
  }
  else
  {        
    StringBuffer temp = new StringBuffer(newAddress);
    for (int count = 0; count < temp.length(); count++)
    {
       char c = temp.charAt(count);
       if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c))
       {
            temp.setCharAt(count, Character.toUpperCase(c));
       }
    }
    address = temp.toString();
  }
}
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

786 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