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

Java读取文本文件到ArrayList并输出失败问题求助

Java学生数据读取与输出问题

编写Java程序读取文本文件中的学生数据并保存到ArrayList,但无法正常输出信息。目标是将每位学生信息存入Student对象,再加入ArrayList后展示内容。已导入java.util.*和java.io.*。

Student类代码

public class Student {
      public String Name;
      public int BirthYear;
      public int BirthMonth;
      public int BirthDay;
      public double GPA;

      
       public Student(String Name,int BirthYear, int BirthMonth, int BirthDay,double GPA)
       {
          Name = this.Name;
          BirthYear = this.BirthYear;
          BirthMonth =this.BirthMonth;
          BirthDay=this.BirthDay;
          GPA=this.GPA;
       }
    
    public int Age() {
           Calendar c = Calendar.getInstance();
           if(c.get(Calendar.MONTH)>BirthMonth) {
               return (c.get(Calendar.YEAR)-BirthYear);
           }
           else if(c.get(Calendar.MONTH)<BirthMonth) {
               return (c.get(Calendar.YEAR)-BirthYear)-1;
           }
           else if(c.get(Calendar.MONTH)==BirthMonth) {
               if(c.get(Calendar.DAY_OF_MONTH)>BirthDay) {
                   return(c.get(Calendar.YEAR)-BirthYear)-1;
               }
               else {
                   return(c.get(Calendar.YEAR)-BirthYear);  
               }
           }
           else {
               return 0; 
           }
       }
}

RunStudent主类代码

public class RunStudent {

    public static void main(String[] args) throws FileNotFoundException{
        File inputFile = new File("studentData1");
        Scanner FileIn = new Scanner(inputFile);
        ArrayList<Student> List = new ArrayList<Student>();
        
        FileIn.nextLine();
        while(FileIn.hasNextLine()) {
            String FName, LName;
            FName = FileIn.next();
            LName = FileIn.next();
            int BirthYear = FileIn.nextInt();
            int BirthMonth = FileIn.nextInt();
            int BirthDay = FileIn.nextInt();
            double GPA = FileIn.nextDouble();
            String name = FName + " " + LName;
            Student s = new Student(name, BirthYear, BirthMonth, BirthDay, GPA);
            List.add(s);
        }
        FileIn.close();
        for(int i=0; i<List.size(); i++) {
            System.out.println("Name:" + List.get(i).Name);
            System.out.println("Age:" + List.get(i).Age());
            System.out.println("GPA: " + List.get(i).GPA);
        }

    }

}

文件与输出情况

  • 文本文件:第一行是表头,后续每行依次为学生名、姓、出生年、月、日、GPA
  • 当前输出:姓名显示null,年龄计算异常,GPA显示0.0

问题根源

  1. 构造函数赋值颠倒:Student类构造函数中,错误地将成员变量赋值给参数(如Name = this.Name),导致传入的参数值无法存入对象属性,所有属性保留默认值(String为null,int为0,double为0.0)。
  2. 年龄计算逻辑错误:Calendar.MONTH字段从0开始计数(1月对应0,12月对应11),但文本文件中的月份是1-12的常规数值,直接比较会导致生日判断错误,年龄计算偏差。

修复方案

修正后的Student类

public class Student {
    public String Name;
    public int BirthYear;
    public int BirthMonth;
    public int BirthDay;
    public double GPA;

    // 修正构造函数赋值方向
    public Student(String Name, int BirthYear, int BirthMonth, int BirthDay, double GPA) {
        this.Name = Name;
        this.BirthYear = BirthYear;
        this.BirthMonth = BirthMonth;
        this.BirthDay = BirthDay;
        this.GPA = GPA;
    }

    public int Age() {
        Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        int currentMonth = c.get(Calendar.MONTH) + 1; // 转换为1-12的月份格式
        int currentDay = c.get(Calendar.DAY_OF_MONTH);

        int age = currentYear - BirthYear;
        // 未到当年生日,年龄减1
        if (currentMonth < BirthMonth || (currentMonth == BirthMonth && currentDay < BirthDay)) {
            age--;
        }
        return age;
    }
}

主类优化建议

主类逻辑基本正确,可优化变量名(如List改为studentList,符合Java命名规范),同时确保studentData1文件路径正确(可放在项目根目录或使用绝对路径)。

修复后程序可正确读取并输出学生的姓名、年龄和GPA信息。

内容的提问来源于stack exchange,提问作者student9876

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 15:47:38