Hello all from javaDumb! I'm in my first java class and this is my first post so please be patient. I'm suppose to use StringTokenizer to search a document and replace some text, but the book for this course only explains how to print the text to the screen as tokens. I have created a class called editor with two constructors; one that has no parameters and one that passes one parameter(the document to be searched). I also have methods for getDocument(), setDocument(). This project also calls for two overloaded methods called replace(), one passes (search text, replace text) and the other passes (document, search text, replace text). I don't know how to use these and StringTokenizer together.

Any help would be appreciated.

Dani AI

Generated

Quick practical guide for (and others): reading the whole document into a String (as suggested) is fine, but StringTokenizer by itself will drop delimiters unless you tell it to return them. If the assignment requires StringTokenizer, rebuild the text token-by-token while preserving delimiters; otherwise prefer a regex or String.replace-based approach for whole-word, safe replacements. If you’re operating on a Swing text component, run edits on the Document inside the Event Dispatch Thread and read the caret/selection inside the method (this addresses the “pos1”/modal-dialog issue ran into).

Example using StringTokenizer while keeping delimiters (whole-word matches depend on your delimiter set):

public String replaceUsingTokenizer(String doc, String search, String replacement) {
    StringBuilder out = new StringBuilder(doc.length());
    String delim = " \t\n\r\f.,;:!?()[]{}\"'<>-";
    StringTokenizer st = new StringTokenizer(doc, delim, true); // return delimiters
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.equals(search)) out.append(replacement);
        else out.append(token);
    }
    return out.toString();
}

Cleaner and safer (recommended): regex with word boundaries and proper escaping:

public String replaceWholeWord(String doc, String search, String replacement) {
    return doc.replaceAll("\\b" + Pattern.quote(search) + "\\b",
                          Matcher.quoteReplacement(replacement));
}

For a Swing JTextPane use the Document and do the full update on the EDT:

SwingUtilities.invokeLater(() -> {
    try {
        Document d = textPane.getDocument();
        String original = d.getText(0, d.getLength());
        String updated = replaceWholeWord(original, search, replacement);
        d.remove(0, d.getLength());
        d.insertString(0, updated, null);
    } catch (BadLocationException e) { e.printStackTrace(); }
});

Notes and troubleshooting: choose delimiters carefully (tokenization can accidentally join words or leave punctuation attached), use equalsIgnoreCase or (?i) in regex for case-insensitive search, and always call Pattern.quote / Matcher.quoteReplacement to avoid regex/replace surprises. For large documents, prefer streaming or Document-aware incremental edits rather than repeated string concatenation.

Recommended Answers

All 5 Replies

Hello all from javaDumb! I'm in my first java class and this is my first post so please be patient. I'm suppose to use StringTokenizer to search a document and replace some text, but the book for this course only explains how to print the text to the screen as tokens. I have created a class called editor with two constructors; one that has no parameters and one that passes one parameter(the document to be searched). I also have methods for getDocument(), setDocument(). This project also calls for two overloaded methods called replace(), one passes (search text, replace text) and the other passes (document, search text, replace text). I don't know how to use these and StringTokenizer together.

Any help would be appreciated.

u can read the whole amount of text in doc together and

pass it to u r fun. ther u can use stringtokenizer to get each string from ur
whole content string and den u can use replace fun of string

Thank you tigerxx for the reply, but I'm still quite lost on how this is done.

Hi everyone,
I am currently trying to do what you are doing now
see this thread

http://www.daniweb.com/techtalkforums/thread14899.html

but the thing is that function that i wrote only runs perfectly once and it can't search
anyting after calling that function.

It's really weird but you can use that function if you like and if you happen to be able to find solution to my problem please let me know as i really need help for that

Yours Sincerely

Richard West

I'll give it a try freesoft_2000, will let you know if i have any better luck.

Thank you,

Kevin Ervin

hi everyone,
Kevin i managed to do it. Firstly do not pass the pos1(the Jtextpane's current position) into the function but read it inside the function. That's why my function was working only once as i was always reading the same position. By the way if you are using a dialog make sure it it not modal and always to grab the focus of your text component.

Yours Sincerely

Richard West

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.