-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStatisticsProcessor.java
More file actions
159 lines (141 loc) · 7.77 KB
/
FileStatisticsProcessor.java
File metadata and controls
159 lines (141 loc) · 7.77 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;
public class FileStatisticsProcessor {
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(() -> {
try {
JOptionPane.showMessageDialog(
null,
" CSC 229 – Project 3\n"
+ " File Processing\n"
+ "__________________________________________________________________\n"
+ " Description: This program reads a text file of integer data\n"
+ " and creates an output text file with the same\n"
+ " information, plus minimum, maximum, total,\n"
+ " and average of data.\n\n"
+ " Input: A text file of integer values, file size is the\n"
+ " first data item on the first line.\n\n"
+ " Output: Entire content of input file, 10 data items per\n"
+ " line plus statistics (minimum, maximum, total,\n"
+ " average (one per line)).\n"
+ "__________________________________________________________________\n",
"Project 3 – Hauser",
JOptionPane.INFORMATION_MESSAGE);
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Open Data File");
chooser.setFileFilter(new FileNameExtensionFilter("Text Data Files", "txt"));
chooser.setAcceptAllFileFilterUsed(false);
String inputFilePath;
String inputFileName;
String outputFilePath;
String outputFileName;
int returnVal = chooser.showOpenDialog(null);
if (returnVal != JFileChooser.APPROVE_OPTION) {
System.exit(0);
return;
}
File inputFile = chooser.getSelectedFile();
inputFilePath = inputFile.getPath();
inputFileName = inputFile.getName();
chooser.setDialogTitle("Save Output File");
returnVal = chooser.showSaveDialog(null);
if (returnVal != JFileChooser.APPROVE_OPTION) {
System.exit(0);
return;
}
File outputFile = chooser.getSelectedFile();
outputFilePath = ensureTxtExtension(outputFile.getPath());
outputFileName = new File(outputFilePath).getName();
int fileSize;
int fileMinimum = Integer.MAX_VALUE;
int fileMaximum = Integer.MIN_VALUE;
long fileSum = 0L;
double fileAverage;
try (Scanner in = new Scanner(new File(inputFilePath));
PrintWriter out = new PrintWriter(outputFilePath)) {
in.useLocale(Locale.US);
if (!in.hasNextInt()) {
showError("The first value in the file must be an integer file size.");
return;
}
fileSize = in.nextInt();
if (fileSize <= 0) {
showError("File size must be a positive integer.");
return;
}
out.println(Integer.toString(fileSize));
int itemOnLine = 0;
int count = 0;
while (count < fileSize) {
if (!in.hasNextInt()) {
showError("Input ended before reading " + fileSize + " integers.");
return;
}
int value = in.nextInt();
out.print(value);
count++;
itemOnLine++;
if (itemOnLine == 10) {
out.println();
itemOnLine = 0;
} else if (count < fileSize) {
out.print(" ");
}
fileSum += value;
if (value < fileMinimum) fileMinimum = value;
if (value > fileMaximum) fileMaximum = value;
}
if (itemOnLine != 0) {
out.println();
}
fileAverage = ((double) fileSum) / fileSize;
out.println(fileMinimum);
out.println(fileMaximum);
out.println(fileSum);
out.println(String.format(Locale.US, "%.2f", fileAverage));
JOptionPane.showMessageDialog(
null,
" CSC 229 - Project 03\n"
+ " File Processing\n"
+ "__________________________________________________________________\n"
+ " Input file name : " + inputFileName + "\n"
+ " Output file name : " + outputFileName + "\n"
+ " File size : " + fileSize + "\n"
+ "__________________________________________________________________\n"
+ " Statistics\n"
+ "__________________________________________________________________\n"
+ " Minimum : " + fileMinimum + "\n"
+ " Maximum : " + fileMaximum + "\n"
+ " Total : " + fileSum + "\n"
+ " Average : " + String.format(Locale.US, "%.2f", fileAverage) + "\n"
+ "__________________________________________________________________",
"Project 03 - Hauser",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
showError("An error occurred: " + e.getMessage());
} finally {
System.exit(0);
}
});
}
private static void showError(String message) {
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
}
private static String ensureTxtExtension(String path) {
int dot = path.lastIndexOf('.');
int sep = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
boolean hasExt = dot > sep;
if (!hasExt) {
return path + ".txt";
}
return path;
}
}