6 Years
Discussion Span
Last Post by Vreality

The second example here is a search through the text. all you would need to do is read the .txt file and store it as a string. then display it in the text field to search through.


What would be a quick example of a search code snippet? I don't understand how to make it search text automatically.
For example, the user inputs text when asked. A before specified text file is called up and converted into a string. The text the user inputted it converted into lowercase, split into separate searches by spaces, and searched for throughout the newly created string that holds the text document.
How does the program search? Is there a specific command or is there a large amount of code to be written?


You want each letter to be searched for? or do you want to search multiple words?

The user would enter their word. Then you store the word. Then once the text document opens up you add the variable to the spot. In the example that I showed you you would insert it into the position of s here

        String s = entry.getText();
        if (s.length() <= 0) {
            message("Nothing to search");
        String content = textArea.getText();
        int index = content.indexOf(s, 0);
        if (index >= 0) {   // match found
            try {
                int end = index + s.length();
                hilit.addHighlight(index, end, painter);
                message("'" + s + "' found. Press ESC to end search");
            } catch (BadLocationException e) {
        } else {
            message("'" + s + "' not found. Press ESC to start a new search");
    void message(String msg) {

You would replace all the s and then it would work like you were searching for it. You could remove some of the code possible, but the idea is the same.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.