4

I have made a phone bill system takes input of phone number called, date of the call and call length. it the saves it in to a text file. what i have not been able to do is search the text file for the phone number.

My coding

1
  • 1
    It looks like you can read in the file. Thus do that, build a list of call objects and maybe put them into a map of phonenumber to list of calls. Then look up/search the map for a number. Commented Mar 29, 2012 at 13:10

3 Answers 3

4

just do linear search (iterating over your phone list) :

public static List<Phone> searchPhone(final String phoneNumber, final List<Phone> phoneList) { List<Phone> matchedPhone = new ArrayList<Phone>(); for(Phone phone: phoneList) { if ( phone.getphoneNumber().equals(phoneNumber) ) { matchedPhone.add(phone); } } return matchedPhone; } 

also for readability, don't make your method parameter as output. its not good practice, so you should change your method from:

static void readList(List<Phone> phoneListIn) { } 

to:

 static List<Phone> readList(final String fileName) { } 

output arguments should be avoided as possible as you can

Sign up to request clarification or add additional context in comments.

2 Comments

for (Phone Phone, phoneList); comes with an error for the line that ';' expected. Although it already has it.
sorry i use different syntax for foreach loop (this is Qt sytanx) , i updated the answser now, you can use foreach loop or normal loop. for (int i=0; i<phoneList.size(); i++) { /* your code here */ }
2

Here is a link to some code that I found using Google.

http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/

Comments

2

Perform Collections.sort(List list);

Then perform Collections.binarySearch(List list, Object key). This should achieve the searching efficiently.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.