import java.util.Scanner; /** Matcher illustrates the following methods in the String class: * indexOf (note there are 4 overloaded versions of indexOf * which essentially searches a string for either a character or a String */ public class Matcher { public static void main(String[] args) { String sentence = "I am looking for a needle in a haystack."; String pattern = "need"; int loc = sentence.indexOf(pattern); System.out.println(pattern + " occurs at loc " + loc + " in " + sentence); char ch = 'a'; loc = sentence.indexOf(ch, 10); System.out.println("a occurs in " + sentence + " at loc " + loc); loc = sentence.indexOf('c'); System.out.println("c occurs in " + sentence + " at loc " + loc); // lets say I want to print all occurrences of say the char 'a' for (int i=0; i