Solved

Problem converting String to Title Case

Posted on 2003-11-26
11
367 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
fibonacci ten numbers 4 45
hibernate insert example 13 34
ejb message driven bean mdb creation steps 2 17
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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