2011-01-31 33 views
0

輸入一行文本。沒有標點符號。UpperCase問題,使用IndexOf

Java是語言。

我已將該行改爲:

是語言java。

嘗試:

int x; 
String sentence, first; 

System.out.println("\nEnter a line of text. No punctuation please."); 

Scanner keyboard = new Scanner (System.in); 

sentence=keyboard.nextLine(); 

    x = sentence.indexOf(" "); 
first= sentence.substring(0,x); 
second=sentence.substring(0,1) 
second=second.toUpperCase(); 
System.out.println("I have rephrased that line to read:"); 
System.out.println(second+sentence.substring(x+1)+" "+first); 

輸出:

輸入一行文本。沒有標點符號。

是怎麼回事

我已經改寫上面一行改爲://它應該閱讀「是怎麼回事什麼」

W¯¯回事什麼

PS -I需要做信「我」資本。 如何讓「second.substring(0,1)」讀取字符「i」? 正如所建議的,我試圖找出剝離的信件,並用大寫連接它 ,但我不確定。

給抨擊我的人不要自己做這件事。我給了它多次嘗試,並通讀這本書。這沒有幫助。我不得不問教授,所以他給了我一個開始,但仍然不夠。無論如何,我想了解它。不是每個人都以同樣的方式掌握。我個人需要示例和詳細解釋來理解。

+3

爲什麼現在他們做這麼傻的家庭作業? – 2011-01-31 17:46:10

+0

這是功課嗎?我們喜歡標記與家庭作業有關的問題。 – 2011-01-31 17:47:10

回答

2

變化:

first= sentence.substring(0,4); 

要:

first= sentence.substring(0,x); 

然後加:

sentence = sentence.substring(x+1,sentence.length()); 
1

嘗試:

first = sentence.substring (0, x); 
// other stuff 
System.out.println (sentence.substring (x + 1) + " " + first); 
0

TL; DR:使用其他請致電substring拆除第一部分。見the String class documentationManipulating Characters in a String


似乎您的代碼並沒有在所有的工作。您保存在x第一空間的位置但不使用它,它應該是sentence.substring(0, x)

對於這句話:你好嗎。在first你所儲存的第一個字:如何

那麼你應該從sentence剝離的第一個字像這樣的東西:sentence = sentence.substring(x+1)。現在,一句是:

之後正好連接兩個:

String newSentence = sentence + " " + first; 

有你有存放在變量newSentence

神如何祝福!