In: Computer Science
The input is broken into chunks of consecutive lines, where each pair of consecutive chunks is separated by a line containing "----snip----". Read the entire input and break it into chunks C1,…,Ck. Then output the chunks in reverse order Ck,…,C1 but preserving the order of the lines within each chunk. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Part7 { /** * Your code goes here - see Part0 for an example * @param r the reader to read from * @param w the writer to write to * @throws IOException */ public static void doIt(BufferedReader r, PrintWriter w) throws IOException { // Your code goes here - see Part0 for an example } /** * The driver. Open a BufferedReader and a PrintWriter, either from System.in * and System.out or from filenames specified on the command line, then call doIt. * @param args */ public static void main(String[] args) { try { BufferedReader r; PrintWriter w; if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); w = new PrintWriter(System.out); } else if (args.length == 1) { r = new BufferedReader(new FileReader(args[0])); w = new PrintWriter(System.out); } else { r = new BufferedReader(new FileReader(args[0])); w = new PrintWriter(new FileWriter(args[1])); } long start = System.nanoTime(); doIt(r, w); w.flush(); long stop = System.nanoTime(); System.out.println("Execution time: " + 1e-9 * (stop-start)); } catch (IOException e) { System.err.println(e); System.exit(-1); } } }
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Part7.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part7 {
/**
* Your code goes here - see Part0 for an example
*
* @param r
* the reader to read from
* @param w
* the writer to write to
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
// creating a list of list of strings to store chunks of lines
ArrayList<ArrayList<String>> chunks = new ArrayList<ArrayList<String>>();
// creating a list to store current chunk
ArrayList<String> currentChunk = new ArrayList<String>();
// looping through each line in the input buffer
for (String line = r.readLine(); line != null; line = r.readLine()) {
// checking if line equals "----snip----"
if (line.equals("----snip----")) {
// adding current chunk to chunks list
chunks.add(currentChunk);
// reinitializing currentChunk to store next chunk of lines
currentChunk = new ArrayList<String>();
} else {
// else, adding line to currentChunk
currentChunk.add(line);
}
}
// at the end, if currentChunk is not empty, adding to chunks
if (!currentChunk.isEmpty()) {
chunks.add(currentChunk);
}
// now looping through each chunk in reverse order
for (int i = chunks.size() - 1; i >= 0; i--) {
// looping through each line in current chunk, in normal order,
// displaying it
for (String line : chunks.get(i)) {
System.out.println(line);
}
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from
* System.in and System.out or from filenames specified on the command line,
* then call doIt.
*
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 1e-9 * (stop - start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}