/* Punters Lounge JAVA programming course * * Lesson62.java * * Author : Erik Datapunter * Date : 28/apr/2004 * * Text manipulation example program * */ public class Lesson62 { public static void main(String[ ] arguments) { // string buffers to hold the existing text StringBuffer mytext = new StringBuffer(100); // one string to hols the word to look for String searchletter = arguments[0]; int startsearch = 0; int foundposition = 0; int nrletter = 0; // use the append method to put the text into the buffer mytext = mytext.append(arguments[1]); /* using a while loop * * we search for the letter using the indexOf method, * with each search we move the starting position based on the * position of the previously found letter * * if no (more) letter found then foundposition becomes -1 * * so as long as foundposition is not -1 we have found a letter * */ while ((foundposition = mytext.indexOf(searchletter,startsearch)) != -1) { // another letter found nrletter = nrletter + 1; // move the start position of the search to the letter after the found letter startsearch = foundposition + 1; } System.out.println("Found : "+nrletter); } }