2014-09-11 111 views
2

喲,我正在嘗試用Java編寫一個程序,它只用字符串和indexOf從字符串中分離單詞。我已經制作了這個代碼,它運行得幾乎完美,但是在打印完單詞之後,它會打印出很多空行。使用indexOf&substring字符串在字符串中分離單詞

class wordSep{ 
    public static void main(String args[]){ 
     String line = "The world is full of strangers"; 
     String word = line.substring(0,line.indexOf(" ")); 
     int index = line.length()-1; 
     while(index>=0){ 
      System.out.println(word); 
      line = line.substring(line.indexOf(" ") + 1) + " "; 
      word = line.substring(0,line.indexOf(" ")); 
      index--; 
     } 
    } 
} 

這樣做的結果應該是:

The 
world 
is 
full 
of 
strangers 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 
\\empty line 

我覺得這與我的while循環的條件下做的。我需要輸出沒有這些空行..請幫助,謝謝。

回答

2

更換while(index>=0)while(!word.isEmpty()),擺脫index

+0

謝謝,這個解決了我的問題。 – patmarley 2014-09-11 02:34:14

+0

對不起,我剛剛創建了這個帳戶分鐘前,我如何投票?編輯:哈哈,似乎我已經投了票,什麼是菜鳥。 – patmarley 2014-09-11 02:38:06

+0

@patmarley別人投了這個答案,你需要一些聲望才能投票。看看[鏈接](http://stackoverflow.com/help/privileges) – 2014-09-11 02:44:11

-1

即使您似乎已經解決了問題,仍然會發布此答案。我相信,你的代碼可以被最小化到只需要幾行是這樣的:

String line = "The world is full of strangers"; 
int idxOfNextWord = 0; 
for (int i = 0; i < line.length(); i++) { 
    if(line.charAt(i)==' ') { 
     System.out.println(line.substring(idxOfNextWord, i)); 
     idxOfNextWord = i+1; 
    }    
} 
System.out.println(line.substring(idxOfNextWord)); 
+0

謝謝,這是偉大的有幾行,我很感激。 – patmarley 2014-09-11 02:46:22

+0

這並沒有考慮到硬道理。 – 2014-12-20 22:36:38

+0

@AdamArold,是的。這是最後SOP的目的。 – lxcky 2015-01-05 02:31:28

1

這是偉大的,你的問題就解決了,但如果你嘗試將是巨大的在代碼中使用一些內置函數(如String.split())和庫,因爲它可以減少大量處理,並使代碼更健壯,並且獨立於字符串長度和其他參數。

您的代碼可以優化到這個水平......

class wordSep{ 
    public static void main(String args[]){ 
     String line = "The world is full of strangers"; 
     String[] word = line.split(" "); 
     for(String str: word){ 
      System.out.println(str); 
     } 
    } 
} 

輸出 -

The 
world 
is 
full 
of 
strangers 

現在你也可以使用字符串標記,但不建議任何更多...

閱讀 - >to completely understand how to split Strings in Java on some character, or using some Regex and other parameters

+0

在發佈此問題之前,我已經看到了您的解決方案,如果您的問題適用,我不會問這個問題。我需要拆分字符串而不使用拆分和數組。謝謝,下次仔細閱讀標題。和平! – patmarley 2014-09-15 02:26:39

+0

我很抱歉。我以爲你已經有了一個解決方案,所以我必須向你提出一些不同的建議。 因此,只是爲了改進,如果你沒有硬編碼你的字符串,那麼就有機會獲得'nullPointerException',你可以在下面的鏈接中看到如何避免這種情況。雖然它使用了一些額外的行,但行比代碼的穩定性更不重要.... http://ideone.com/x7Tdks 感謝您的建議.... – 2014-09-16 11:08:24

0

J.幸運,可以減少SOP的數量和大小,以及像這樣:

class Extract 
{ 
    void Substring() 
    {  
     String s = "The World is Full of Strangers."; 
     s = s + " "; 
     int idxOfNextWord = 0; 
     for(int i = 0; i < s.length(); i++) 
     { 
      if(s.charAt(i)== ' ') 
      { 
       System.out.println(s.substring(idxOfNextWord, i)); 
       idxOfNextWord = i+1; 
      }    
     } 
    } 
}