954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search Program

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!

Vreality
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

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.

sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
 

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?

Vreality
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

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.

sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
 

Thanks for answering! I'll try it.

Vreality
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: