Problem: You are to write a single method: public static boolean contains (Strin
ID: 3789291 • Letter: P
Question
Problem: You are to write a single method: public static boolean contains (String lastNaneList. String searchForMe) The method is passed a list of comma separated last names (lastNamelList) as well as a name for which this list should be searched (searchForMe). The method returns trim starch For Me is one of the last names in the list and false if it is not. The search is case sensitive. Each name in lastMameLost is followed by a comma except for the final name in the list which is not followed by a comma. Here is an example: Bursiit.Meyer, Steinnan, Bach each, Marple The method should he called from the main method and its return value displayed on the monitor. Test thoroughly Your program must comply with the requirements of the Style Guide but you need not follow the documentation requirement. Just make sure your full name, section number, date, and a brief description of what the program does are in comments at the beginning of the program (class). What You Must Turn In You must turn in a paper listing of your program. If the program contains multiple pages, the pages must be stapled together.Explanation / Answer
SearchForLastName.java
import java.util.Scanner;
public class SearchForLastName {
public static void main(String[] args) {
//Declaring variable
String searchLastName;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
String lastNameList= "Bursik,Meyer,Steinman,Bachrach,Marple";
//Getting the Lastname entered by the user
System.out.print("Enter the last name you want to search :");
searchLastName=sc.next();
//calling the method by passing the values as arguments
boolean bool=contains(lastNameList,searchLastName);
/* based on the returned value of the contains()
* method display the corresponding message
*/
if(bool==true)
{
System.out.println(searchLastName +" is available in "+lastNameList);
}
else
{
System.out.println(searchLastName +" is not available in "+lastNameList);
}
}
//This method will check whether the user entered lastname is present in the lastname list or not
private static boolean contains(String lastNameList, String searchLastName) {
//Declaring the variable
boolean bool = false;
//Getting the string array
String lastnameArray[]=lastNameList.split(",");
/* This for loop will check whether the user entered
* last name is available in the lastname list or not
*/
for(int i=0;i<lastnameArray.length;i++)
{
if(lastnameArray[i].equals(searchLastName))
{
bool=true;
return bool;
}
else
bool=false;
}
return bool;
}
}
____________________
output1:
Enter the last name you want to search :Williams
Williams is not available in Bursik,Meyer,Steinman,Bachrach,Marple
_____________________
Output2:
Enter the last name you want to search :Bachrach
Bachrach is available in Bursik,Meyer,Steinman,Bachrach,Marple
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.