4)Read the first 50 lines of input and then write them out in reverse order. Rea
ID: 3755678 • Letter: 4
Question
4)Read the first 50 lines of input and then write them out in reverse order. Read the next 50 lines and then write them out in reverse order. Do this until there are no more lines left to read. If the number of lines is not a multiple of 50 then the last batch of lines to be reverses will be however many there are (between 1 and 49). below isthe skeleton file for this package comp2402a1;
package comp2402a1;
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 Part4 {
/**
* 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: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
Explanation / Answer
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.Stack;
public class Part4 {
/**
Read the first 50 lines of input and then write them out in reverse order.
Read the next 50 lines and then write them out in reverse order.
This is done until there are no more lines left to read.
Reading in reverse order can be easily achieved through Stack since it is LIFO structure.
@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 {
Stack s = new Stack();
String line = r.readLine();
while (line != null) {
s.push(line);
for (int i = 1; i < 50; i++) {
line = r.readLine();
if (!(line == null)) {
s.push(line);
} else {
break;
}
}
int count=s.size();
for (int i = 0; i < count; i++) {
line = (String) s.pop();
w.println(line);
}
line = r.readLine();
}
}
/**
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: " + 10e-9 * (stop - start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
Reading in reverse order can be achieved using stack since it is LIFO (Last In First Out)
Modified code has been highlighted
input.txt
0 abc
1 abc
2 abc
3 abc
4 abc
5 abc
6 abc
7 abc
8 abc
9 abc
10 abc
11 abc
12 abc
13 abc
14 abc
15 abc
16 abc
17 abc
18 abc
19 abc
20 abc
21 abc
22 abc
23 abc
24 abc
25 abc
26 abc
27 abc
28 abc
29 abc
30 abc
31 abc
32 abc
33 abc
34 abc
35 abc
36 abc
37 abc
38 abc
39 abc
40 abc
41 abc
42 abc
43 abc
44 abc
45 abc
46 abc
47 abc
48 abc
49 abc
50 abc
51 abc
52 abc
53 abc
54 abc
55 abc
56 abc
57 abc
58 abc
59 abc
------------------------------------------------------------------
output.txt
49 abc
48 abc
47 abc
46 abc
45 abc
44 abc
43 abc
42 abc
41 abc
40 abc
39 abc
38 abc
37 abc
36 abc
35 abc
34 abc
33 abc
32 abc
31 abc
30 abc
29 abc
28 abc
27 abc
26 abc
25 abc
24 abc
23 abc
22 abc
21 abc
20 abc
19 abc
18 abc
17 abc
16 abc
15 abc
14 abc
13 abc
12 abc
11 abc
10 abc
9 abc
8 abc
7 abc
6 abc
5 abc
4 abc
3 abc
2 abc
1 abc
0 abc
59 abc
58 abc
57 abc
56 abc
55 abc
54 abc
53 abc
52 abc
51 abc
50 abc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.