?
Solved

Problem converting String to Title Case

Posted on 2003-11-26
11
Medium Priority
?
380 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
[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
  • 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
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!

 
LVL 92

Accepted Solution

by:
objects earned 1000 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

Hire Technology Freelancers with Gigs

Work with 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

765 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