-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextIO.java
52 lines (40 loc) · 1.4 KB
/
TextIO.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
import java.util.*;
public class TextIO {
private Scanner scan;
public TextIO() {
this.scan = new Scanner(System.in);
}
public void display(String msg) {
System.out.println(msg);
}
public int[] calculateColumnWidth(String[][] results) {
int[] rowWidths = new int[results[0].length];
for (int i = 0; i < results[0].length; i++) {
int largestCharacterCount = Integer.MIN_VALUE;
for (int j = 0; j < results.length; j++) {
int characterCount = results[j][i].length();
if (characterCount > largestCharacterCount) {
largestCharacterCount = characterCount;
}
}
rowWidths[i] = largestCharacterCount;
}
return rowWidths;
}
public String formatReport(String[][] results, int padding, int[] columnWidths) {
String report = "";
for (int i = 0; i < results.length; i++) {
for (int j = 0; j < results[0].length; j++) {
String data = results[i][j];
int columnWidth = columnWidths[j];
report += String.format("%-" + (columnWidth + padding) + "s", data);
}
report += "\n";
}
return report;
}
public String prompt(String msg) {
this.display(msg);
return this.scan.nextLine();
}
}