2013-01-05 102 views
2
String word="i love apples i love orange"; 
String w=scan.next(); 
    int index = word.indexOf(w); 
    System.out.println (index); 
while (index >= 0) { 
    System.out.println(index); 
    index = word.indexOf(w, index + 1); 
} 

,所以我知道這個代碼會告訴我愛的指數(2,17) 但什麼即時尋找的是,我希望它返回我的單詞的索引是(1,4),即它計算字符串內的字符串,而不是字符......我也需要它來指示索引,每次它發現它像上面一樣,謝謝指數每次它發生

+5

通過分詞您串和計數。 – assylias

+0

我試過使用循環來循環字符串字長度,但也計數的字符,我不明白爲什麼 –

+0

好,所以我拆分他們使用數組字符串[] a = word.split(「」);那我該如何計算一個用戶輸入的單詞呢? –

回答

2

如果在變量「字」字使用空格僅方式隔開,你可以使用這樣的代碼

String word="i love apples i love orange"; 
String w=scan.next(); 
String[] words = word.split(" "); 
for (int i=0; i< words.length; i++){ 
    if (words[i].equals(w)){ 
     System.out.println(i); 
    } 
} 

UPDATE: 如果你想算的話試試這個 -

String word="i love apples i love orange"; 
String w=scan.next(); 
String[] words = word.split(" "); 
int count = 0; 
for (int i=0; i< words.length; i++){ 
    if (words[i].equals(w)){ 
     System.out.println(i); 
     count ++; 
    } 
} 
System.out.println("Count = "+count); 
0

這個代碼是找到一個字中的輸入和該單詞的字符串中的位置的位置。

public static void main(String[] args) { 
    int lastIndex = 0; 
    Scanner scan = new Scanner(System.in); 
    String w = scan.next(); 

    String word = "i love apples i love orange"; 
    String[] tokens = word.split(" "); 

    for (String token : tokens) { 
     if (token.contains(w)) { 
      for (int x = 0; x < token.length(); x++) { 
       System.out.println("Input found in token at position: " + (token.indexOf(w) + 1)); 
      } 

      System.out.println("Word found containing input in positions: " + (word.indexOf(token, lastIndex) + 1) 
        + "-" + ((word.indexOf(token, lastIndex)) + token.length())); 
      lastIndex = ((word.indexOf(token,lastIndex)) + token.length()); 
     } 
    } 
} 
0

此代碼每次發生時都會在段落(str)內找到字符串(針)。 針可以包含空格,並且每次都會打印單詞索引。

String str = "i love apples i love orange"; 
String needle = "i love"; 
int wordIndex = 0; 
for (int start = 0; start < str.length(); start++) { 
    if (Character.isWhitespace(str.charAt(start))) wordIndex++; 
    if (str.substring(start).startsWith(needle)) { 
    System.out.println(wordIndex); 
    } 
} 
-1
package JavaPractice; 

public class CountNumberOfWords { 
public static void main(String[] args) { 
    String str = "My name is srikanth. srikanth is working on a java program. " + 
      "srikanth dont know how many errors atr there. so, " + 
      "srikanth is going to find it."; 
    String Iwant = "srikanth"; 
    int wordIndex = 0; 
    int count =0; 
    for (int start = 0; start < str.length(); start++) { 
     if (Character.isWhitespace(str.charAt(start))) wordIndex++; 
     if (str.substring(start).startsWith(Iwant)) { 
     System.out.println("Index of the String Iwant "+wordIndex); 
     count++; 
     } 
    } 
    System.out.println("Number of times srikanth in str is="+count); 

} 
} 

輸出:

 
Index of the String Iwant 3 
Index of the String Iwant 4 
Index of the String Iwant 11 
Index of the String Iwant 20 
Number of times srikanth in str is=4 
+1

嘗試格式化代碼以使其可讀。編輯帖子時有設施。因此,你甚至已經回答了這個問題並不是十分清楚。 – Peter