Reputation: 2604
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);
}
so i know that this code will tell me the index of love is (2,17) but what im looking for is that i want it to return for me the index of the word to be (1,4) that is it counts the strings within the string and not the characters...also i need it to indicate the index each time it finds it like the one above thanks
Upvotes: 2
Views: 1098
Reputation: 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);
}
}
Output:
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
Upvotes: -1
Reputation: 3462
This code find the string (needle) inside the paragraph (str) each time it occours. The needle can contain spaces, and the word index is printed each time.
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);
}
}
Upvotes: 0
Reputation: 2371
If in your variable "word" words seperates only with using spaces, you can use such code
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: If you would like to count words try this -
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);
Upvotes: 2
Reputation: 94499
This code finds the position of the input within the word and the position of the word within the string.
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());
}
}
}
Upvotes: 0