Try with
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.next();
Pattern p=Pattern.compile("a");
Matcher matcher=p.matcher(word);
boolean b=false;
while(b=matcher.find())
{
System.out.println(matcher.start()+"");
}
Edit:
Pattern.compile("a");
Compiles the given regular expression into a pattern
p.matcher(word);
Creates a matcher that will match the given input against this pattern.
If you would like to search for source string like aba
then for all occurrence of expression a
, it will do like
source:aba
index:012
we can see two occurences of the expression a: one start from position 0 and second starting from 2. so the output is 0 2