I suppose to write a Java program using array and method follows: It reads a sequence of strings, each on a separate line, and stores them in an array, let call it input1, with one string per cell, in the order they were read. The sequence ends with an empty line: one with a String of length 0. Same thing with 2nd sequence.Then prints the 1st sequence and 2nd sequence. And then create an array that contains all of the elements of the above two arrays. Merging is done by alternating between the arrays: that is, the first cell of input1 is copied followed by the first cell of input2. Then the second cell of input1 is copied followed by the second cell of input2. Of course, in general, the two sequences may have different lengths, so after the shorter sequence is finished, all elements of the longer sequence are simply appended to the output array. Finally, prints the merged array with 1 string each line.
Example:
Input:
Sequence 1 line 1
Sequence 1 line 2
Sequence 2 line 1
Sequence 2 line 2
Sequence 3 line 3
Output:
Sequence 1 line 1
Sequence 1 line 2
Sequence 2 line 1
Sequence 2 line 2
Sequence 3 line 3
Sequence 1 line 1
Sequence 2 line 1
Sequence 1 line 2
Sequence 2 line 2
Sequence 2 line 2
Sequence 2 line 3
Here is what i have so far for read a sequence of string until it finds an empty line. What should i do next? Thanks a lots .
import java.util.Scanner;
public class A4 {
public static void readInput(Scanner myScanner) {
boolean streamEnded = false;
while (!streamEnded && myScanner.hasNext()) {
String seq1 = myScanner.nextLine();
String seq2 = myScanner.nextLine();
if (seq1.length() == 0 && seq2.length() == 0 ) {
streamEnded = true;
} else {
System.out.println(seq1);
System.out.println(seq2);
}
}
}
public static void main(String[] args) {
Scanner aScanner = new Scanner(System.in);
readInput(aScanner);
readInput(aScanner);
readInput(aScanner);
}
}