Westonci.ca is your go-to source for answers, with a community ready to provide accurate and timely information. Discover detailed solutions to your questions from a wide network of experts on our comprehensive Q&A platform. Connect with a community of professionals ready to help you find accurate solutions to your questions quickly and efficiently.

Strings batch1 and batch2 are read from input. Each string consists of at least two foods, separated by " " and surrounded by "[" and "]". Assign firstItems with the first food from each string, separated by " ".

Ex: If the input is:

[peach bread lemon]
[pear carrot cheese]
then the output is:

import java.util.Scanner;

public class JoinMultipleFirsts {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String batch1;
String batch2;
String firstOfBatch1;
String firstOfBatch2;
String firstItems;

batch1 = scnr.nextLine();
batch2 = scnr.nextLine();

int startIndex = batch1.lastIndexOf(' ') + 1;

int endIndex = batch1.length() - 1;

firstOfBatch1 = batch1.substring(startIndex, endIndex);


startIndex = batch2.lastIndexOf(' ') + 1;

endIndex = batch2.length() - 1;

firstOfBatch2 = batch2.substring(startIndex, endIndex);

firstItems = firstOfBatch1 + " " + firstOfBatch2;

System.out.println(firstItems);
}
}