currentWordPos = currentWordPos+newWord.length(); currentWordEnd = getNextWordEnd(text, currentWordPos); nextWordPos = getNextWordStart(text, currentWordEnd); if (nextWordPos == -1) moreTokens = false;
currentWordPos = getNextWordStart(text, currentWordPos+newWord.length()); if (currentWordPos != -1) { currentWordEnd = getNextWordEnd(text, currentWordPos); nextWordPos = getNextWordStart(text, currentWordEnd); } else moreTokens = false;
  public void replaceWord(String newWord) {    if (currentWordPos != -1) {      try {        document.remove(currentWordPos, currentWordEnd - currentWordPos);        document.insertString(currentWordPos, newWord, null);      } catch (BadLocationException ex) {        throw new RuntimeException(ex.getMessage());      }      //Position after the newly replaced word(s)      first = true;      currentWordPos = currentWordPos+newWord.length();      currentWordEnd = getNextWordEnd(text, currentWordPos);      nextWordPos = getNextWordStart(text, currentWordEnd);      if (nextWordPos == -1)        moreTokens = false;    }  }