java - String search with "startsWith()" -


i've been working on email directory program past couple of day , in 1 of methods i'm trying make search function searches email based off character inputs of user. i'm trying make method loops , user types in email 1 character @ time until 1 email in array constructed method.

heres code:

private void searchcontact()     {     string[] newrecords=new string[emailrecords.size()];        //temp array searching     arraylist<string> searchrecords=new arraylist<string>();    //to passed insertion sort     newrecords=emailrecords.toarray(newrecords);                      for(string records: newrecords)      {         scanner search=new scanner(system.in);                  //setup user input         string letter;         string searchval;          system.out.println("please enter first letter of email you're trying find.");         letter=search.nextline();          if (searchrecords.size()!=1)             {                (int i=0; i<newrecords.length;i++)          //counter indexes             {                 searchval=newrecords[i];                        //set temp value set index                  if (searchval.startswith(letter))               //starts boolean                 {                     searchrecords.add(searchval);               //add temp array later comparison                 }             }         }         else             {             break;                                              //break if 1 remains in array.         }     }     system.out.println(searchrecords);                          //todo erase when finalizing } 

and here's happens when run program entering names starting same letter:

please enter number of option choice: 1. add new contact 2. search exsisting contact 3. exit 1 please enter email adress. mark ***mark stored.*** please enter number of option choice: 1. add new contact 2. search exsisting contact 3. exit 1 please enter email adress. mike ***mike stored.*** please enter number of option choice: 1. add new contact 2. search exsisting contact 3. exit 1 please enter email adress. molly ***molly stored.*** please enter number of option choice: 1. add new contact 2. search exsisting contact 3. exit 2 please enter first letter of email you're trying find. m please enter first letter of email you're trying find. please enter first letter of email you're trying find. r [mark, mike, molly] please enter number of option choice: 1. add new contact 2. search exsisting contact 3. exit 

and here expected output after enter in information , try search "mark" entering "m", "a", "r", , "k":

please enter next letter of email you're trying find. m please enter next letter of email you're trying find. please enter next letter of email you're trying find. r please enter next letter of email you're trying find. k [mark] 

i tried make loop on outside of other counts , use move index of given string failed. feel i'm close overlooking something. advise or strategies appreciated! million.

assuming emailrecords contains of emails, task like:

private void searchcontact() {     assert(!(emailrecords == null || emailrecords.isempty()));// :p     //initially copy     arraylist<string> searchrecords = new arraylist<>(emailrecords);     //prepare scanner     scanner search = new scanner(system.in);     //initialize query     string query = "";     //loop:     while (searchrecords.size() > 1) {         system.out.println("please enter first letter of email you're trying find.");         //read input         query += search.nextline();         //iterate through remaining searchrecords         (iterator<string> = searchrecords.iterator(); it.hasnext();) {             final string entry = it.next();             if (!entry.startswith(query)) {//...conditionally                 it.remove();//..remove (from searchrecords)             }         }     }     //print output - first/last of searchrecords     if (!searchrecords.isempty())         system.out.println(searchrecords.get(0));     else         system.out.println("no record found."); } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -