2013-07-04 74 views
1

這是整個問題的SS。 http://prntscr.com/1dkn2e 它應該適用於任何一個句子,而不僅僅是示例 中的句子,我知道它必須對字符串做些什麼。我們的教授已經在與這些字符串方法 http://prntscr.com/1dknco將第一個單詞移到句子結尾?

這僅僅是一個基本的Java類,所以不使用任何複雜的東西 這裏是我,不知道這 任何幫助會後該怎麼辦不勝感激。

public static void main(String[] args) 
{ 
     Scanner keyboard = new Scanner(System.in);  
     System.out.println("Enter a line of text. No punctuaton please"); 
     String sentence = keyboard.nextLine(); 
     System.out.println(sentence); 
    } 
} 

回答

0

以及IT似乎你尋找非常簡單的字符串算術 所以這是最簡單的我能做到:

 // get the index of the start of the second word 
     int index = line.indexOf (' '); 
     // get the first char of the second word 
     char c = line.charAt(index+1); 
     /* this is a bit ugly, yet necessary in order to convert the 
     * first char to upper case */ 
     String start = String.valueOf(c).toUpperCase(); 
     // adding the rest of the sentence 
     start += line.substring (index+2); 
     // adding space to this string because we cut it 
     start += " "; 
     // getting the first word of the setence 
     String end = line.substring (0 , index); 
     // print the string 
     System.out.println(start + end); 
+0

代碼類的工作,這個詞被替換而不是原來的 java是語言 是javajava – xpression

+0

再次檢查你自己,我已經測試過了,現在又做了。「是語言java」是我的輸出 –

+0

是的工作,感謝隊友:D – xpression

2

您可以使用public String[] split(String regex)

splitted = sentence.split("\\s+"); 
  • splitted[0]是第一個字。
  • splitted[splitted.length - 1]是硬道理。

由於站立着不許使用String#split,你可以這樣做伎倆:

myString = myString.substring(0, myString.lastIndexOf(" ")) + firstWord; 

通過這樣做,你就會有一個substring包含句子不硬道理。 (對於提取的第一個字,你可以使用String#indexOf

firstWord是之前(我不會解決整個問題對你來說,嘗試自己做它,你提取的第一個字,它現在應該很容易)

+0

不知道你的意思。這種方法不在這裏http://prntscr.com/1dknco 你可以將該代碼合併到我的? – xpression

+0

@xpression看我的編輯。 – Maroun

+0

myString = myString.substring(0,myString.lastIndexOf(「」))+ firstWord; eclipse中的firstWord是一個錯誤,不確定你的意思是由String#indexOf。我們可能會按照你上次展示的方式去做。我想我們的教授只是忘記告訴我們方法 記得我只是一個初學者,截至目前我們只有兩個班,所以我仍然感到困惑。 – xpression

0

試試這個

String str = "Java is the language"; 
String first = str.split(" ")[0]; 
str = str.replace(first, "").trim(); 
str = str + " " + first; 
System.out.println(str); 
+0

注意他要求在sentace開頭的第一個新字符將是大寫字母。 –

-1

這是另一種可以做到這一點的方法。 更新:沒有循環

   Scanner keyboard = new Scanner(System.in);  
     System.out.println("Enter a line of text. No punctuaton please"); 
     String sentence = keyboard.nextLine(); 
     System.out.println(sentence); 
     int spacePosition = sentence.indexOf(" "); 
     String firstString = sentence.substring(0, spacePosition).trim(); 
     String restOfSentence = sentence.substring(spacePosition, sentence.length()).trim(); 
     String firstChar = restOfSentence.substring(0, 1); 
     firstChar = firstChar.toUpperCase(); 
     restOfSentence = firstChar + restOfSentence.substring(1, restOfSentence.length()); 
     System.out.println(restOfSentence + " " + firstString); 
     keyboard.close(); 
+0

我會考慮這個非常糟糕的代碼。我不會向初學者展示這樣的東西。 – Paranaix

+0

我認爲這是一個初學者代碼,它只是基本的循環,我不認爲這是任何高級代碼。不同的人不同的意見,我會說。 – Peshal

+0

雖然它很複雜,但如果真的想要使用循環,可以簡化該示例。例如'if(sentence.charAt(i)=='')firstWord = sentence.substring(0,i);' – Paranaix

相關問題