How would you create a basic search engine? Could this search engine also search based upon tags?
(Not searching a site or database but just a .txt document)
Thanks in advance!

Recommended Answers

All 4 Replies

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

hilit.removeAllHighlights();
         
        String s = entry.getText();
        if (s.length() <= 0) {
            message("Nothing to search");
            return;
        }
         
        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);
                textArea.setCaretPosition(end);
                entry.setBackground(entryBg);
                message("'" + s + "' found. Press ESC to end search");
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        } else {
            entry.setBackground(ERROR_COLOR);
            message("'" + s + "' not found. Press ESC to start a new search");
        }
    }
 
    void message(String msg) {
        status.setText(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.

Thanks for answering! I'll try it.

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.