2011-01-20 109 views
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 
    } 
} 

輸入:丹尼爾天劉易斯如何結合InputStreamReader和StringBuffer?

預期輸出:

丹尼爾天路易斯= ... - >輸入字符串的容量

丹尼爾天路易斯= ... - >輸入的長度串

「丹尼爾·戴 - 劉易斯」 改爲 「丹尼爾·戴·劉易斯」


+1

什麼是你的問題?如何大寫單詞的字符串?從消息中讀取後續行? – 2011-01-20 09:19:56

+0

請幫助我.. ?? – fortysant 2011-01-20 09:23:01

+1

@fortysant,您的標題顯然與代碼中隱藏的問題不匹配。請再次編輯它,改變標題並添加幾個詞來描述你真正需要的東西。一個示例用於輸入和期望輸出的字符串也會有幫助! – 2011-01-20 09:33:57

回答

1

你是什麼題?我不明白!你

一個想法問題的標題是這樣的:

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

但我不明白你的問題。 問候。

2

如果你不想重新發明輪子(這總是一個好的舉動),你應該使用肖恩·帕特里克·弗洛伊德對Apache Commons的建議。


如果你想只使用核心Java,你可能會感興趣的java.text.BreakIterator製作自己的toTitleCase功能。例如,「Zach L」將被BreakIterator解釋爲[「Zach」,「」,「L」](括號僅用於強調)。

在我們的自定義toTitleCase功能,這需要一個名爲string作爲參數字符串,我們可以遍歷使用的BreakIterator每個「字」,建立了這樣的事情:

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()) 
{ 

我們調好參數,字符串,稍後爲了方便起見而使用小寫字母。通過在startend之間進行子串匹配來獲取子詞,並將它們連接或附加到我們返回的字符串中,並不難。 (作爲一點額外提示,你介意在start的角色上使用Character.toUpper有幫助)。

儘管如此,它可能更好地使用肖恩·帕特里克·弗洛伊德的建議。這只是作爲一種選擇提供。


以供將來參考,這是更好地把你的問題,問題文本,而不是僅僅在你的代碼中的註釋。否則,我們很多第一反應可能是你沒有問一個真正的問題。