BACKGROUND
I have a string of text and a hash set that contains words that i am looking for.
Given
String doc = "one of the car and bike and one of those";
String [] testDoc = doc.split("\\s+");
HashSet<String> setW = new HashSet<>();
setW.add("and");
setW.add("of");
setW.add("one");
OBJECTIVE
The objective is to scan the string and each time we come across a word that is in the hash set we are to store the word and the position of the starting index.
In the above case we should be able to store the following
one-->0
of-->4
and-->15
and-->24,
one-->28,
of-->32
` ATTEMPT
//create hashmap
for(int i = 0; i<testDoc.length; i++){
if(setW.contains(testDoc[i])) {
doc.indexOf(testDoc[i]);
//add string and its index to hashmap
}
That is what i have thought of so far the only problem is that the indexOf method only looks at the first occurrence of a word so i am not sure what to do. If i keep trimming the string after each word scanned then i will not the get index position of a word in the original string.
I would love some input here.