Originally posted by Ernest Friedman-Hill:
You want to use a "for" loop, and loop over all the characters in the String; check each one to see if it's "a", and if it is, add 1 to a counter variable. At the end, the counter has the value you want.
If you want to show that you can think outside the box and you're using a recent enough version of
Java, there are a couple other ways to do it:
Use String.replaceAll() and length(). What should the regexp be?Use String.split() and {array}.length. What should THIS regexp be? If you're really dying to use indexOf(), look at the indexOf(int, int) version and use that in, as Mr. Friedman-Hill suggested, a "for" loop. What would you use for the second int in each call? What is the terminating condition for the "for" loop?
[ July 12, 2006: Message edited by: Ryan McGuire ]