/** * Returns searches for the next word in the text, and returns that word. * * @return the string representing the current word. * @throws WordNotFoundException search string contains no more words. */ public String nextWord() { currentWord = finder.next(); return currentWord.getText(); }
/** * Replace the current word in the search with a replacement string. * * @param newWord the replacement string. * @throws WordNotFoundException current word has not yet been set. */ public void replace(String newWord) { if (currentWord == null) { throw new WordNotFoundException("No Words in current String"); } StringBuffer sb = new StringBuffer(text.substring(0, currentWord.getStart())); sb.append(newWord); sb.append(text.substring(currentWord.getEnd())); int diff = newWord.length() - currentWord.getText().length(); currentWord.setText(newWord); /* Added Conditional to ensure a NullPointerException is avoided (11 Feb 2003) */ if (nextWord != null) { nextWord.setStart(nextWord.getStart() + diff); } text = sb.toString(); sentenceIterator.setText(text); int start = currentWord.getStart(); sentenceIterator.following(start); startsSentence = sentenceIterator.current() == start; }