Ask Question

Write a method called findNames that meets the following specs: It takes two arguments: a list of strings, allNames, and a string, wanted Creates and returns a new list containing only the strings from, allNames, that contain the name stored in wanted as a substring. List allNames should remain unmodified: do not delete, or modify the names in it. This should work in a case insensitive manner, but the names in your result list should be exactly as they are in A (i. e. not converted to lower case). We referred to allames and what the method retums as 'lists', but you are free to represent cach one of them however you want: array or ArrayList. No error checking needed. Example behavior: if allNames is: ["Bob Smith", "Elroy Jetson", "Christina Johnson", "Rachael Baker", "Chris Conly"] and wanted is "cHRis" the resulting list, should be: ["christina Johnson", "Chris Conly"] because only those two strings contain "chris"

+1
Answers (1)
  1. 21 December, 21:29
    0
    public class Solution {

    public static void main (String args[]) {

    String[] allNames = new String[]{"Bob Smith", "Elroy Jetson", "Christina Johnson", "Rachael Baker", "cHRis", "Chris Conly"};

    String searchString = "cHRis";

    findNames (allNames, searchString);

    }

    public static void findNames (String[] listOfName, String nameToFind) {

    ArrayList resultName = new ArrayList ();

    for (String name : listOfName) {

    if (name. toLowerCase (). contains (nameToFind. toLowerCase ())) {

    resultName. add (name);

    }

    }

    for (String result : resultName) {

    System. out. println (result);

    }

    }

    }

    Explanation:

    The class was created called Solution. The second line define the main function. Inside the main function; we initialized and assign an array called allNames to hold the list of all name. Then a String called searchString was also defined. The searchString is the string to search for in each element of allNames array. The two variables (allNames and searchString) are passed as argument to the findNames method when it is called.

    The method findNames is defined and it accept two parameters, an array containing list of names and the name to search for.

    Inside the findNames method, we initialized and assigned an ArrayList called resultName. The resultName variable is to hold list of element found that contain the searchString.

    The first for-loop goes through the elements in the allNames array and compare it with the searchString. If any element is found containing the searchString; it is added to the resultName variable.

    The second for-loop goes through the elements of the resultName array and output it. The output is empty if no element was found added to the resultName variable.

    During comparison, the both string were converted to lower case before the comparison because same lowercase character does not equal same uppercase character. For instance 'A' is not same as 'a'.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a method called findNames that meets the following specs: It takes two arguments: a list of strings, allNames, and a string, wanted ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers