At Westonci.ca, we provide clear, reliable answers to all your questions. Join our vibrant community and get the solutions you need. Discover precise answers to your questions from a wide range of experts on our user-friendly Q&A platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

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);
}
}



Sagot :