forked from jdeisenberg/javads
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
KeyTerms.java
56 lines (51 loc) · 1.69 KB
/
KeyTerms.java
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
/*
* Given a file of key terms for a chapter,
* create a PreTeXt table with three items per row.
*/
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class KeyTerms {
public static void main(String[] args) {
int nColumns = 3;
if (args.length < 1 || args.length > 2) {
System.out.println("Usage: java KeyTerms inputfile nColumns");
} else {
String fileName = args[0];
if (args.length == 2) {
nColumns = Integer.parseInt(args[1]);
}
ArrayList<String> terms = new ArrayList<>();
try {
Scanner inFile = new Scanner(new File(fileName));
while (inFile.hasNextLine()) {
terms.add(inFile.nextLine());
}
inFile.close();
}
catch (Exception ex) {
System.out.println(ex);
}
System.out.println("""
<table>
<tabular>""");
int nRows = (terms.size() + nColumns -1) / nColumns;
System.err.println("n rows: " + nRows);
for (int row = 0; row < nRows; row++) {
System.out.println(" <row>");
for (int col = 0; col < nColumns; col++) {
int itemNumber = col * nRows + row;
String str = "";
if (itemNumber < terms.size()) {
str = terms.get(itemNumber);
}
System.out.println(" <cell>" + str + "</cell>");
}
System.out.println(" </row>");
}
System.out.println("""
</tabular>
</table>""");
}
}
}