开发者

How to combine InputStreamReader with StringBuffer?

开发者 https://www.devze.com 2023-02-05 10:46 出处:网络
import java.io.*; public class MyName { public static void main (String [] xxx) throws IOException { String a;
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"



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:

  • WordUtils.capitalize(String)
  • WordUtils.capitalizeFully(String)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消