0
import java.io.*;

public class MyName
{
    public static void main (String [] xxx) throws IOException
    {
        String a;

        BufferedReader read = new BufferedReader (new InputStreamReader (System.in));

        System.out.print("Input A Name : ");
                    a = read.readLine();
        System.out.print("\n");
        System.out.println("Your Name : " +a);
        System.out.println("Name Capacity: " +a.capacity());
        System.out.println("Length Of A Name : "+a.length());

        /*After the first word of the name (if the name is more than one word) 
                  then the first letter of each word must in uppercase*/
        System.out.println((a)+" change to = ".....); //i have no idea what code to wite
    }
}

Input: daniel day lewis

Expected output :

daniel day lewis = ... -> capacity of input string

daniel day lewis = ... -> length of input string

"daniel day lewis" changed to "daniel Day Lewis"


laurent
  • 88,262
  • 77
  • 290
  • 428
fortysant
  • 11
  • 2
  • 5
  • 1
    What is exactly your question? How to capitalize words un a String? Reading subsequent lines from the message? – Tomas Narros Jan 20 '11 at 09:19
  • 1
    @fortysant, your title apparently does not match the question that is somewhat hidden in the code. Please edit it again, change title and add a few words to describe what you really need. An example Strings for input and expected output would be helpful too! – Andreas Dolk Jan 20 '11 at 09:33
  • You want to capitalise a set of words in a string? eg. "daniel day lewis" -> "Daniel Day Lewis" – Dunes Jan 20 '11 at 09:34
  • @Andreas : my title is "how to combine InputStreamReader with StringBuffer".... @Dunes : yes.. – fortysant Jan 20 '11 at 09:42
  • This might be a duplicate of [Capitalize First Char of Each Word in a String Java](http://stackoverflow.com/questions/1892765/capitalize-first-char-of-each-word-in-a-string-java) – Zach L Jan 20 '11 at 10:08

3 Answers3

2

If you use Apache Commons / Lang, you can do this:

String capitalized = WordUtils.capitalize(orig);

And if you want to enforce Title Case (e.g. no other Upper Case letters except the initials), you can do this (thanks @manoj):

String capitalized = WordUtils.capitalizeFully(orig);
// almost equivalent to WordUtils.capitalize(orig.toLowerCase());

Reference:

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I think CapitalizeFully() is better to avoid any typos and accidental upper case in the middle of the words. – Manoj Jan 20 '11 at 09:46
2

If you don't want to reinvent the wheel (which is always a good move), you should use Sean Patrick Floyd's suggestion for Apache Commons.


If you want to use just core Java, you might be interested in java.text.BreakIterator for making your own toTitleCase function. BreakIterator.getWordInstance() can be used to substring specific words:

For example, "Zach L" would be interpreted by BreakIterator as ["Zach", " ", "L"] (brackets used just for emphasis).

In our custom toTitleCase function, which takes a String called string as a parameter, we could loop over each 'word' using a BreakIterator, set up something like this:

BreakIterator words = BreakIterator.getWordInstance();
string = string.toLowerCase();
words.setText(string);
int start = words.first();
for (int end = words.next(); end != BreakIterator.DONE; 
     start = end, end = words.next())
{

We set our parameter, string, to lower case just for ease later. It shouldn't be too hard from here to get the subwords by substringing between start and end, titlecase the words, and concatenate or append them to the String we return. (As a little extra hint, you mind find it helpful to use Character.toUpper on the character at start).

Once again, though, it's probably much better to use Sean Patrick Floyd's suggestion. This is just offered as an alternative.


For future reference, it's better to put your problem in the question text, rather than just as comments in your code. Otherwise, a lot of our first reactions might be that you aren't asking a real question.

Zach L
  • 16,072
  • 4
  • 38
  • 39
  • thank u for the code and thanks for the advice... i'm a newbie in java code...and i'm from indonesia so i'm sorry if my english not well.... – fortysant Jan 20 '11 at 10:15
  • +1 for the BreakIterator solution. Haven't used this class yet! – Tomas Narros Jan 20 '11 at 10:18
  • @fortysant: We all have to start as newbies (I'm still pretty much a newbie myself) at some point. Good luck with Java! – Zach L Jan 20 '11 at 10:20
1

What is your question? I did not understand!

One idea of you question title is this:

StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String s;
while(s=br.readLine() != null) {
    sb.append(s);
}

But I do not understand you problems. Greetings.

Israel
  • 3,252
  • 4
  • 36
  • 54