0

I swear I did this a while back and it worked. But I can't remember how I did it. I want to find the indexes of multiple words in a string. The code below only works for one word. I need a more eloquent solution for dealing with several.

import java.util.*;

public class test {

    public static List<List<Boolean>> work;

    public static void main (String[] args) {
        String test = "I'm trying to both extract and replaces. This that and the other";
        String word = "I'm";

        for (int i = -1; (i = test.indexOf(word, i + 1)) != -1; ) {


            System.out.println(i);
        }
    }
}

Let's say I wanted to find the index of “both” and “other.”

T145
  • 1,415
  • 1
  • 13
  • 33
  • What is a *multipliable word*? "Multipliable" means "able to be multiplied", and "multiply" is normally a math operation in computing, though it can also mean copied/repeated. Neither interpretation seems to fit here. – Andreas Sep 06 '16 at 01:08
  • What is field `work` used for? --- What if a word occurs multiple times, e.g. if you wanted index of "and". Do you want both indexes or just the first? --- When you search for multiple words at the same time, do you need to know which word was at which index, or do you just want a list of index values? – Andreas Sep 06 '16 at 01:11

4 Answers4

0

buddy ... I was trying to point you in the right direction ... not give you complete working code :) Here is a complete working code:

public static void main (String[] args) 
{
    String test = "I'm trying to both extract and replaces. This that and the other";
    String[] words = {"I'm", "other", "both"};

    for (int j=0; j < words.length; j++) 
    {           
        System.out.println("Word: " + words[j] + " is at index: " + test.indexOf(words[j]));
    }
}

This way, you will not get ArrayIndexOutOFBounds exception. You are going through each word once.

But for each word, you have go through entire test string once. In your earlier code, you were using i = test.indexOf(word) in the loop. If the words were not in the order of appearing in test string, this will create problem.

For eg: test = "I'm a rockstar" words = {"rockstar", "a"} In this case, the loop variable i moves forward to index where 'rockstar' is found but then will not find 'a' after that as test string doesn't contain an 'a' after 'rockstar'.

But in the current code I posted, order of words can be anything.

Hope its clear.

0

I did some more research, this appears to be what you are asking for:

String test = "I'm trying to both extract and replaces. This that and the other";

    String[] words = test.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        words[i] = words[i].replaceAll("[^\\w]", "");
    }

    System.out.println(Arrays.asList(words).indexOf("both"));

Result: 3

Keep in mind that Java counts including 0, so "both" is actually at index 4 for us humans.

----Old Solution----

The question is vague about what they want, but this will return the index number corresponding to each word via HashMap:

HashMap<Integer, String> hmap = new HashMap<Integer, String>();
    String test = "I'm trying to both extract and replaces. This that and the other";

    String[] words = test.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        words[i] = words[i].replaceAll("[^\\w]", "");
    }

    for(int i = 0; i < words.length; i++){
        hmap.put(i, words[i]);
    }

    System.out.println(hmap);

Result:

{0=Im, 1=trying, 2=to, 3=both, 4=extract, 5=and, 6=replaces, 7=This, 8=that, 9=and, 10=the, 11=other}

  • Question says *"I wanted to find the **index** of “both” and “other”"*. You're finding the word index (3), not the index position (14). – Andreas Sep 06 '16 at 01:14
0

I believe you're referencing the answer from this SO question. To use it for more than one word, all you need is a simple data structure to store the words you want to find, loop over that, and return a map holding the word and its corresponding locations, as follows:

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class WordFinder {

    public static void main(String[] args) {
        String phrase = "0123hello9012hello8901hello7890";
        String[] words = { "hello", "90" };

        Map<String, Stack<Integer>> dest = findWordsInString(words, phrase);

        System.out.println(dest.toString());
    }

    public static Map<String, Stack<Integer>> findWordsInString(String[] words, String phrase) {
        Map<String, Stack<Integer>> dest = new HashMap<>();

        for (String word : words) {
            Stack<Integer> locations = new Stack<>();

            for (int loc = -1; (loc = phrase.indexOf(word, loc + 1)) != -1;) {
                locations.add(loc);
            }

            dest.put(word, locations);
        }

        return dest;
    }
}

Program Output: {90=[9, 19, 29], hello=[4, 13, 22]}

Community
  • 1
  • 1
T145
  • 1,415
  • 1
  • 13
  • 33
0
public static void main (String[] args)
{
    String test="I'm trying to both extract and replaces. This that and the other";
    String[] words={"other","This","that"};
    for(int j=0;j<words.length;j++)
    {
        for(int i=-1;(i=test.indexOf(words[j],i+1))!=-1;)
        {
            System.out.println(words[j]+" "+ i);
        }
    }
}

ouput

other 59
This 41
that 46
Hao Chen
  • 1
  • 1