You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
2.0 KiB
44 lines
2.0 KiB
import java.io.BufferedReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
|
|
public class ScoreCalculator {
|
|
public static void main(String[] args) {
|
|
// 使用try-with-resources确保流自动关闭
|
|
try (BufferedReader reader = new BufferedReader(new FileReader("scores.txt"))) {
|
|
String line;
|
|
int lineNumber = 0;
|
|
while ((line = reader.readLine()) != null) {
|
|
lineNumber++;
|
|
try {
|
|
// 处理每行数据
|
|
String[] parts = line.split(",");
|
|
if (parts.length < 2) {
|
|
throw new IllegalArgumentException("第" + lineNumber + "行数据格式错误: 缺少必要字段");
|
|
}
|
|
|
|
String studentId = parts[0].trim();
|
|
if (studentId.isEmpty()) {
|
|
throw new IllegalArgumentException("第" + lineNumber + "行数据格式错误: 学号为空");
|
|
}
|
|
|
|
try {
|
|
double score = Double.parseDouble(parts[1].trim());
|
|
System.out.println("学号: " + studentId + ", 成绩: " + score);
|
|
} catch (NumberFormatException e) {
|
|
System.err.println("第" + lineNumber + "行数据格式错误: 成绩不是有效数字 - " + e.getMessage());
|
|
}
|
|
} catch (IllegalArgumentException e) {
|
|
System.err.println(e.getMessage());
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
System.err.println("文件不存在: " + e.getMessage());
|
|
} catch (IOException e) {
|
|
System.err.println("文件读取错误: " + e.getMessage());
|
|
} catch (Exception e) {
|
|
System.err.println("发生未知错误: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|