logo Practice-It logo

reverseAndFlip

Language/Type: Java file processing Scanner
Author: Marty Stepp (on 2020/04/16)

Write a static method called reverseAndFlip that takes a Scanner containing an input file as a parameter and that writes to System.out the same file with successive pairs of lines reversed in order and with the second line of each pair reversed. For example, if the input file contains the following:

        Twas brillig and the slithy toves
        did gyre and gimble in the wabe.
        All mimsey were the borogroves,
        and the mome raths outgrabe.
        
        "Beware the Jabberwock, my son,
        the jaws that bite, the claws that catch,
        Beware the JubJub bird and shun
        the frumious bandersnatch."

The method switches the order of the first two lines, printing the second line reversed. Then it switches the order of the third and fourth lines, print the fourth line reversed. And so on. It should produce this output:

        .ebaw eht ni elbmig dna eryg did
        Twas brillig and the slithy toves
        .ebargtuo shtar emom eht dna
        All mimsey were the borogroves,
        ,nos ym ,kcowrebbaJ eht eraweB"
        
        nuhs dna drib buJbuJ eht eraweB
        the jaws that bite, the claws that catch,
        the frumious bandersnatch."

Notice that a line can be blank, as in the third pair. Also notice that an input file can have an odd number of lines, as in the one above, in which case the last line is printed in its original position. You may not make any assumptions about how many lines are in the Scanner and you may not construct any extra data structures to solve this problem.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.