Write a method called playListBuilder that takes no parameters and returns an Ar
ID: 3815815 • Letter: W
Question
Write a method called playListBuilder that takes no parameters and returns an ArrayList of Strings (song names). In the method body, declare an ArrayList of Strings called playList and populate it with song names (Strings using input from the user at the command line. The user can enter as many songs as they like Use a sentinel to allow the user to indicate when they are finished Don't forget to include a prompt. Write a method called searchPlaylist that takes an ArrayList of Strings as a parameter (the playList) and returns a boolean. In the method body, prompt the user to enter a song name the search term a string Then check if the song name appears in the playlist ArrayList. If it is found, print "song found otherwise, print "Song not found".Explanation / Answer
Answer:
public static ArrayList<String> playListBuilder() {
ArrayList<String> playList = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the song name (quit to exit): ");
String name = scan.nextLine();
while(!name.equalsIgnoreCase("quit")) {
playList.add(name);
System.out.println("Enter the song name (quit to exit): ");
name = scan.nextLine();
}
return playList;
}
public static boolean searchPlayList(ArrayList<String> playList) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the song name to search: ");
String name = scan.nextLine();
for(String song: playList){
if(song.equalsIgnoreCase(name)){
return true;
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.