You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java读取student.dat计算GPA时异常处理相关作业问题

Java学生GPA计算作业修正方案

基础问题确认

  • 作业不需要接收用户手动输入学分和质量分,所有数据全部从students.dat文件读取,你代码里的grade类、Scanner scan = new Scanner(System.in)都是冗余代码,可以直接删除
  • 你现有异常捕获逻辑完全错位,不需要额外创建g:\students.dat的读取逻辑,所有文件操作、数据解析逻辑都要包裹在try-catch结构中
  • 循环内多余打印的问题是因为你把不需要循环执行的代码放到了while块里,调整代码位置即可解决

修正后完整代码

// Warning.java Java Foundations
// Reads student data from a text file and writes data to another text file.
// ****************************************************************************
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class Warning {
    public static void main (String[] args) {
        int creditHrs = 0;         // 已获得学期学分数
        double qualityPts = 0;     // 总质量分
        double gpa = 0;            // GPA
        String name = null;
        int recordIndex = 0; // 记录当前处理的学生记录序号
        DecimalFormat df = new DecimalFormat("0.###"); // 格式化对象无需循环创建,放到循环外
        
        Scanner inFile = null;
        try {
            // 替换为你本地students.dat的实际路径,注意Java路径反斜杠需要转义为双反斜杠,或用正斜杠
            inFile = new Scanner(new File("C:\\Users\\patti\\Desktop\\Patricksdcom101class\\CSIT 210\\students.dat"));
            System.out.println ("\n   Students on Academic Warning:\n");
            
            // 逐行处理文件内容
            while (inFile.hasNext()) {
                recordIndex++;
                try {
                    name = inFile.next();
                    creditHrs = Integer.parseInt(inFile.next());
                    qualityPts = Double.parseDouble(inFile.next());
                    
                    // 避免除零异常
                    if(creditHrs == 0) {
                        System.out.printf("第%d条记录异常:%s的学分不能为0%n", recordIndex, name);
                        continue;
                    }
                    // 计算GPA
                    gpa = qualityPts / creditHrs;
                    
                    // GPA低于2.0输出姓名和GPA
                    if (gpa < 2.0) {
                        System.out.println(name + "\t" + df.format(gpa));
                    }
                } catch (NumberFormatException e) {
                    System.out.printf("输入文件格式错误,第%d条记录解析失败%n", recordIndex);
                    // 跳过当前错误记录的剩余字段,避免后续逻辑混乱
                    if(inFile.hasNextLine()) inFile.nextLine();
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("错误:指定的students.dat文件不存在,请检查文件路径是否正确");
        } finally {
            // 确保文件流正常关闭
            if(inFile != null) {
                inFile.close();
            }
        }
    }
}

关键修改说明

  • 异常逻辑调整:
    • 把文件初始化逻辑包裹在try块中,直接捕获FileNotFoundException,删除了冗余的额外文件读取逻辑
    • 在循环内部给数据解析代码包裹NumberFormatException捕获逻辑,通过recordIndex计数器输出出错的记录序号,同时增加了除零异常的兼容处理
  • 冗余代码删除:移除了无用的grade类实例、用户输入的Scanner对象,把DecimalFormat初始化移到循环外避免重复创建对象
  • 逻辑优化:增加了错误记录跳过逻辑,某条记录解析失败后不会影响后续记录的读取,增加了finally块保证文件流正常关闭

内容的提问来源于stack exchange,提问作者Patrick O'Rourke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 18:45:00