Reputation: 43
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.
Upvotes: 4
Views: 911
Reputation: 1169
Perform Collections.sort(List list);
Then perform Collections.binarySearch(List list, Object key).
This should achieve the searching efficiently.
Upvotes: 2
Reputation: 4340
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
Upvotes: 4
Reputation: 989
Here is a link to some code that I found using Google.
http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/
Upvotes: 2