Solved

Problem converting String to Title Case

Posted on 2003-11-26
11
340 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
> 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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> but it throws the  syntax error

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

Author Comment

by:Beam
Comment Utility
Sorry Objects just got ya last version and it works great.


Thanks for your time guys.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
Happy we could help :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now