Design a java program that reads a file containing a list of integers, and calculates the average of those integers. The program should handle any exceptions that may occur during file input/output or calculation of the average, and provide appropriate error messages to the user. Additionally, the program should allow the user to specify the file name and path as a command line argument when running the program. This program takes a command line argument specifying the file name, path, and reads the file line by line using a BufferedReader. It attempts to parse each line as an integer using Integer.parseInt(), and adds it to a running sum and count. If a line is not a valid integer, it prints an error message but continues processing the rest of the file. If the file is empty or does not contain any valid integers, it prints an error message. Finally, it calculates the average and prints it to the console. The program also handles various exceptions that may occur during file input/output or calculation of the average, and prints appropriate error messages to the user.
时间: 2024-02-05 13:03:27 浏览: 96
数据结构:第5章数组和广义表A.ppt
Here's an example Java program that meets the requirements you've described:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class AverageCalculator {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java AverageCalculator <filename>");
System.exit(1);
}
String filename = args[0];
int sum = 0;
int count = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
try {
int num = Integer.parseInt(line);
sum += num;
count++;
} catch (NumberFormatException e) {
System.err.println("Invalid integer: " + line);
}
}
if (count == 0) {
System.err.println("No valid integers found in file.");
} else {
double average = (double) sum / count;
System.out.println("Average: " + average);
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
} catch (ArithmeticException e) {
System.err.println("Error calculating average: " + e.getMessage());
}
}
}
```
Here's how the program works:
1. It checks that the user has provided a single command line argument specifying the filename.
2. It initializes variables to keep track of the sum and count of integers in the file.
3. It creates a BufferedReader to read the file line by line.
4. For each line, it attempts to parse it as an integer using Integer.parseInt(). If successful, it updates the sum and count variables. If not, it prints an error message but continues processing the rest of the file.
5. After processing the entire file, it checks whether any valid integers were found. If not, it prints an error message. Otherwise, it calculates the average and prints it to the console.
6. If any exceptions occur during file input/output or calculation of the average, the program catches them and prints appropriate error messages to the user.
阅读全文