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.
3 Answers
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
2 Comments
Abu Zubayr Ibn Mohamed
for (Phone Phone, phoneList); comes with an error for the line that ';' expected. Although it already has it.
Wajdy Essam
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 */ }
Here is a link to some code that I found using Google.
http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/