Write a method named flipLines that accepts as its parameter a Scanner for an in
ID: 3588474 • Letter: W
Question
Write a method named flipLines that accepts as its parameter a Scanner for an input file and that writes to the console the same file's contents with successive pairs of lines reversed in order. For example, if the input file contains the following text: 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 program should print the first pair of lines in reverse order, then the second pair in reverse order, then the third pair in reverse order, and so on. Therefore your method should produce the following output to the console: did gyre and gimble in the wabe. Twas brillig and the slithy toves and the mome raths outgrabe. All mimsey were the borogroves, "Beware the Jabberwock, my son, Beware the JubJub bird and shun 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. Type your solution here:Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SuccessivePairsReverse {
StringBuilder sb=new StringBuilder();
public StringBuilder flipLines(Scanner s){
try
{
s = new Scanner(new File("reverse.txt"));
while(s.hasNextLine()){
String line = s.nextLine();
System.out.println(line);
sb.append(line).append("/");
}
//exceptions
} catch (FileNotFoundException ex){
System.out.println("This file does not exist");
}
return sb;
}
public static void main(String[] args) {
SuccessivePairsReverse srev=new SuccessivePairsReverse();
StringBuilder sb=srev.flipLines(new Scanner(System.in));
System.out.println();
System.out.println("Successive pair's reverse order. ");
//System.out.println(sb);
String[] successsiveReverseArray=sb.toString().split("/");
for(int i=0;i<successsiveReverseArray.length-1;i=i+2)
{
if(successsiveReverseArray[i].equals("")){
i=i-1;
System.out.println();
}else{
System.out.println(successsiveReverseArray[i+1]);
System.out.println(successsiveReverseArray[i]);
}
}
// System.out.println("length: "+successsiveReverseArray.length);
}
}
/* output:-
Twas brilling and the slithy toves
did gyre and gimble in the wabe.
All mimsey were the borogroves,
and the more raths outgrabe.
"Beware the Jabberwock, my son,
The jaws that bite, the claws that catch,
Beware the Jubjub bird and shun
The frumious Bandersnatch."
Successive pair's reverse order.
did gyre and gimble in the wabe.
Twas brilling and the slithy toves
and the more raths outgrabe.
All mimsey were the borogroves,
The jaws that bite, the claws that catch,
"Beware the Jabberwock, my son,
The frumious Bandersnatch."
Beware the Jubjub bird and shun
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.