views:

106

answers:

2

I need to implement a search feature into my program. At present it reads from a file one line at a time, like so:

   public void importContacts() {
     try {
     BufferedReader infoReader =
                 new BufferedReader(new FileReader("../files/Contacts.txt"));
        txtName.setText(readLine(infoReader));
        txtPhone.setText(readLine(infoReader));
        txtMobile.setText(readLine(infoReader));
        txtAddress.setText(readLine(infoReader));

How do I get the program to search for a name in within the file and show it in my Jtextfields??

full code here: http://pastebin.com/d12aa3ab3

+1  A: 

I'd recommend creating an object to represent your input (like a Contact) and populate it as you read your file. Then, you will be able to add methods for retrieving the name from your class and compare it to a search string.

When you find the Contact that matches your query, simply return it and have a method in your form such as setActiveContact(Contact c) that can read the fields from your object and update your form.

You could go even further and create a ContactLibrary that you can execute methods on to return matches. For example, you could have a query(String q) method that returns a list of contacts matching your request. Then, display these in a list box and as the user selects a contact, update fields with the information.

jheddings
Lucene would be an overkill for a homework.. :) (no -1, because it is actually a good suggestion)
Bozho
i think i wouldn't be allowed to use this? although i was never told, how o you use it?
addiosamigo
@Bozho: yeah, the homework tag was added after I answered.
jheddings
A: 

I'm going to be the evil one here and suggest you take a look at regular expressions. Take a look at the syntax and if you have the intestinal fortitude to continue, start hacking together an expression. Note that it's not for the faint-of-heart and not a good solution for this problem (except that it's homework and pretty much anything that works is usually accepted).

spork
thanks man, i think this is a little too advanced so i'm not gonna bother with it!thanks
addiosamigo
-1 Let people learn to walk before they run. The OP doesn't know the basics about Java yet. Why don't you suggest a solution more relevant to the level of experience. For example the OP need to learn how to write a loop first. All the data has already been read into an ArrayList, So all you need to do is loop through the ArrayList to find the search word. Even if you use a Regex, you still need to master the basics of writing a loop. Using a Regex doesn't help you read data from a file.
camickr
camickr, the question was how to search a text file, not how to write a loop - and the OP says nothing about having already read data into an ArrayList. Other posters gave information about supporting classes and logic. I brought up regular expressions because a regex pattern is essentially a search. I pointed out to the OP that it is not a good practical solution and that it may be more in-depth than he wants to go. Regardless, I do not see an answer to this post from you.
spork