提问人:student9876 提问时间:9/19/2023 最后编辑:student9876 更新时间:9/19/2023 访问量:55
如何从java中的文本文件中获取文件输入,然后使用数组列表显示它?
How to get file input from a text file in java and then display it using an array lists?
问:
当我运行从文本文件获取文件输入然后将此输入保存到数组列表中的程序时,我无法打印信息。我将附加我的 Student 类,这是我用于数组列表的类型。我还将附加包含 main 的类。我将包括文本文件的屏幕截图和我当前获得的输出。我的目标是将每个学生的信息保存到一个学生对象中,然后将这些对象放入数组列表中。然后显示数组列表的内容。 下面是学生班级。
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;
}
}
}
下面是主类
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);
}
}
}
我还导入了 java.util.* 和 java.io.*,但它不允许我将其包含在帖子中。
答:
2赞
Reilas
9/19/2023
#1
"...我想显示学生的姓名、年龄和 GPA”
Student 类未保留所提供信息的原因是构造函数方法中的代码。
您分配的字段不正确。
this 关键字引用类实例上下文,而不是作用域上下文。
Name = this.Name;
BirthYear = this.BirthYear;
BirthMonth =this.BirthMonth;
BirthDay=this.BirthDay;
GPA=this.GPA;
翻转这些分配。
this.Name = Name;
this.BirthYear = BirthYear;
this.BirthMonth = BirthMonth;
this.BirthDay = BirthDay;
this.GPA = GPA;
输出
Name:David King
Age:18
GPA: 3.211
Name:Jane Hall
Age:21
GPA: 3.9662
Name:Mary Reed
Age:19
GPA: 2.8
Name:John Diaz
Age:20
GPA: 2.916
下面是针对代码的一些建议。
只需减去日历值即可获得年份值。
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
return y < 0 ? 0 : y - BirthYear;
或者,使用 Period 对象。
LocalDate bd = LocalDate.of(BirthYear, BirthMonth, BirthDay);
Period p = Period.between(LocalDate.now(), bd);
return Math.max(p.getYears(), 0);
将小数值(即 GPA)存储为 String 或 BigDecimal 对象。
此外,我会在 Scanner#next 调用中对每一行使用 String#split。
String[] s;
int y, m, d;
FileIn.nextLine();
while(FileIn.hasNextLine()) {
s = FileIn.nextLine().split(" +");
y = Integer.parseInt(s[2]);
m = Integer.parseInt(s[3]);
d = Integer.parseInt(s[4]);
List.add(new Student(s[0] + ' ' + s[1], y, m, d, s[5]));
}
评论