Answer:
see explaination
Explanation:
import java.util.*;
import java.util.concurrent.*;
class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Scanner Object to read Input
//LinkedQueue of String Type
ConcurrentLinkedQueue<String> cat_queue = new ConcurrentLinkedQueue<String>();
ConcurrentLinkedQueue<String> dog_queue = new ConcurrentLinkedQueue<String>();
System.out.println("Enter Dog names and Cat names\n");
//Reading input until line exists
while(sc.hasNextLine()){
String s = sc.nextLine(); //Reading string
String[] arr = s.split("\\s+"); //Splitting the string based on spaces
if (arr[0].equals("q") && arr[1].equals("quit")){ //checking if q quit or not
//printing cats and dogs if q quit
System.out.println("\ncats:");
//printing each cat_name in seperate line
for (String cat_name : cat_queue) { //Iterating over elements in cat_queue
System.out.println(cat_name);
}
System.out.println("\ndogs:");
//printing each dog_name in seperate line
for (String dog_name : dog_queue) { //Iterating over elements in dog_queue
System.out.println(dog_name);
}
System.exit(0); //Exiting as q quit is entered
}
else{
if(arr[0].equals("c")){ //If c is the string entered before name
cat_queue.add(arr[1]); //Add the cat_name into cat_queue
}
else if(arr[0].equals("d")){ //If d is is the string entered before name
dog_queue.add(arr[1]); //Add the dog_name into dog_queue
}
}
}
}
}
see attachment for screenshot of the code and output