I have my code that will read in a a list of numbers in a file called "QuizScores.txt". However, in this file, certain numbers contain letters. I have told my program to simply ignore those instances and move onto the next number. My issue now if that my code is only reading one line at a time where I need it to look at the entire file, read it in, calculate the average, the maximum, and the minimum, and finally output that to a file called "QuizStats.txt". Any help with this would be greatly appreciated! Thanks!
QuizScores:
45
63
74g
34.7
75
4
8
15
16
23
42
67f
34
67
Code:
import java.io.*;
public class ScoreReader {
public static void main(String[] args) {
BufferedReader reader = null;
try {
String currentLine;
reader = new BufferedReader(new FileReader("QuizScores.txt"));
while ((currentLine = reader.readLine()) != null) {
int sum = 0;
String[] nums = currentLine.split("\\s+");
for (int i = 0; i < nums.length; i++) {
try{
double num = Integer.parseInt(nums[i]);
if (num != -1) {
sum += num;
}
} catch( NumberFormatException err )
{
}
}
System.out.println(sum);
}
} catch (IOException err) {
err.printStackTrace();
}
catch (NumberFormatException err) {}
finally {
try {
if (reader != null){
reader.close();
}
}
catch (IOException err) {
err.printStackTrace();
}
}
}
}