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 21:03:12 浏览: 29
Sure, here's a sample Java program that meets your requirements:
```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.trim());
sum += num;
count++;
} catch (NumberFormatException e) {
System.err.println("Invalid integer on line: " + line);
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
System.exit(1);
}
if (count == 0) {
System.err.println("No valid integers found in file.");
System.exit(1);
}
double average = (double) sum / count;
System.out.printf("Average of %d integers is %.2f\n", count, average);
}
}
```
To use this program, you would compile it using `javac AverageCalculator.java`, and then run it using `java AverageCalculator <filename>`, where `<filename>` is the name and path of the file containing the integers to be averaged. For example, if the file is called `numbers.txt` and is located in the same directory as the program, you would run `java AverageCalculator numbers.txt`.
The program first checks that the correct number of command line arguments were provided. If not, it prints an error message and exits. Otherwise, it reads in the file using a `BufferedReader`, and attempts to parse each line as an integer using `Integer.parseInt()`. If the 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 and exits.
Finally, the program calculates the average by dividing the sum by the count, 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.