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



Answer :

Other Questions