-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintTwice.java
More file actions
64 lines (56 loc) · 1.35 KB
/
PrintTwice.java
File metadata and controls
64 lines (56 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Name: Rishi Saravanan
//Date: 1/29/22
//Program Name: PrintTwice
//Program Goal: Print File twice in different ways using files and printWriter
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
public class PrintTwice {
Scanner input = null;
public static void main(String[] args) {
PrintTwice p1 = new PrintTwice();
p1.runIt();
}
public void runIt() {
String[] rows = new String[7];
tryCatchIt();
int counterRow = 0;
int counterWords = 0;
while (input.hasNext()) {
rows[counterRow] = input.nextLine();
counterRow++;
}
for (int x = rows.length - 1; x >= 0; x--) {
System.out.println(rows[x]);
}
tryCatchIt();
while (input.hasNext()) {
input.next();
counterWords++;
}
String[] words = new String[counterWords];
counterWords = 0;
tryCatchIt();
while (input.hasNext()) {
words[counterWords] = input.next();
counterWords++;
}
System.out.println();
for (int y = words.length - 1; y >= 0; y--) {
System.out.print(words[y] + " ");
}
input.close();
}
public void tryCatchIt() {
String inFileName = "input.txt";
input = null;
try {
File inFile = new File(inFileName);
input = new Scanner(inFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("Cannot find " + inFileName + " file.");
}
}
}