Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java search a text file

Tags:

java

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

like image 963
Abu Zubayr Ibn Mohamed Avatar asked Feb 22 '23 00:02

Abu Zubayr Ibn Mohamed


1 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

like image 161
Wajdy Essam Avatar answered Mar 03 '23 02:03

Wajdy Essam