0

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

    }

}
2
Contributors
1
Reply
9
Views
3 Years
Discussion Span
Last Post by JamesCherrill
0

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.
...
Same thing with 2nd sequence.

You have tried to combine these two steps into one loop, and forgotten the arrays. The instructions seem to be well written to guide you step-by-step through this task, so it's best to follow them exactly, one step at a time.
Start by just reading a single sequence of strings into an array called input1.

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.