Solved

Problem converting String to Title Case

Posted on 2003-11-26
11
376 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

717 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