-3

So I've split a string into an array, and I want to ask the user for a word to search for, search the array for the chosen word and output every location of the word. However, the indexOf function doesn't seem to be able to search the array? Any correction I could make?

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    String str = "Java String to String Array Example";

    String strArray[] = str.split(" ");
    String word;
    int baby;

    System.out.println("Please enter a message");
    word = scan.nextLine();

    baby = strArray.indexOf(word);

    while (baby >= 0) {
        System.out.println("The word occurs at index " + baby);

        baby = strArray.indexOf(word, baby + word.length());

        for (int counter = 0; counter < strArray.length; counter++) {
            System.out.println(strArray[counter]);
        }
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • String [] data type doesn't have indexOf method. Check this link. http://stackoverflow.com/questions/6249537/indexof-in-a-string-array – CrazyJavaLearner Jun 16 '16 at 16:17
  • Arrays don't have any methods, other than those inherited from `Object`. Which part of the compiler error message is confusing about that? Write a `for` loop. – Andreas Jun 16 '16 at 16:19

2 Answers2

0

You can get starting index of every matching word using regex.
Please see this example:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    String str = "Java String to String Array Example";

    String strArray[] = str.split(" ");
    String word;
    int baby;

    System.out.println("Please enter a message");
    word = scan.nextLine();

    ArrayList<Integer> positions = new ArrayList();
    Pattern p = Pattern.compile(word);
    Matcher m = p.matcher(str);
    while (m.find()) {
        System.out.println("Occurs at position: " + m.start());
        positions.add(m.start());
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
PVR
  • 885
  • 9
  • 18
0

First the indexOf method does not exist for a simple array - it is a method of the List interface which is implemented by ArrayList.

The index you are storing in the int baby is actually not the index of the word in the string but the word count - i.e. 0 would be 1st word, 1 would be second word.

The indexOf method stops at the first occurrence so is not well suited to what I think you are trying to do.

This will do what I think you want:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String str = "Java String to String Array Example";
    List<String> strArray = Arrays.asList(str.split(" "));

    System.out.println("Please enter a message");
    String word = scan.nextLine();

    for (int i = 0; i < strArray.size(); i++)
        if (strArray.get(i).equals(word))
            System.out.println(word + " found at location " + i);

}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
BigMac66
  • 1,528
  • 5
  • 19
  • 35
  • Why do you convert to a list when you don't use list functionality? You could keep the array instead and loop through the array. – Tobias Brösamle Jun 16 '16 at 17:05
  • True - just left over code when I was working with OP use of the indexOf() method. You are entirely correct no need for a List. – BigMac66 Jun 17 '16 at 16:27