java - 比较文本文件中类似字符串的行
问题描述
我有一个看起来像这样的文本文件:
6
3.3 John Rodgers
3.9 Jim Braash
3.5 Kathy Calderon
3.2 Steve Hernandez
2.4 Stacy Lu
2.8 Faith Simmons
我已经写了一个Student
类,它具有基本功能:
package com.company;
public class Student {
private String firstName;
private String lastName;
private double grades;
public Student(String firstName, String lastName, double grades) {
this.firstName = firstName;
this.lastName = lastName;
this.grades = grades;
}
@Override
public String toString() {
return lastName + ", " + firstName + ", " + grades;
}
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
Student other = (Student) obj;
if (other.firstName.equals(this.firstName) && other.lastName.equals(this.lastName) && other.grades == this.grades) {
return true;
} else {
return false;
}
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public double getGrade() {
return this.grades;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setGrades(double grades) {
this.grades = grades;
}
}
这是我的Main
课:
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Student[] s = initialize();
Student max = maxIndex(s);
Student min = minIndex(s);
double avg = avg(s);
flush(max, min, avg);
}
public static void flush(Student max, Student min, double avg) throws FileNotFoundException {
DecimalFormat df = new DecimalFormat("#.#");
double avgFormatted = Double.parseDouble(df.format(avg));
PrintWriter writer = new PrintWriter("final.txt");
writer.write("Highest: " + max);
writer.write("\n");
writer.write("Lowest: " + min);
writer.write("\n");
writer.write("Average GPA: " + avgFormatted);
writer.close();
}
public static Student[] initialize() throws FileNotFoundException {
Scanner reader = new Scanner(new File("data.txt"));
int size = reader.nextInt();
Student[] students = new Student[size];
int index = 0;
while (reader.hasNextLine()) {
double grades = reader.nextDouble();
String firstName = reader.next();
String lastName = reader.next();
Student student = new Student(firstName, lastName, grades);
students[index] = student;
index++;
}
return students;
}
public static double avg(Student[] students) {
double avg = 0;
double sum = 0;
for (int i = 0; i < students.length; i++) {
sum += students[i].getGrade();
avg = sum / students.length;
}
return avg;
}
public static Student maxIndex(Student[] students) {
int max = 0;
for (int i = 1; i < students.length; i++) {
if (students[i].getGrade() > students[max].getGrade()) {
max = i;
}
}
return students[max];
}
public static Student minIndex(Student[] students) {
int min = 0;
for (int i = 1; i < students.length; i++) {
if (students[i].getGrade() < students[min].getGrade()) {
min = i;
}
}
return students[min];
}
}
所以,我的问题涉及处理文件。假设我再次将名称 Jim Braash 添加到我的文件中,而没有更改顶部的整数。所以我的文件看起来像这样:
6
3.3 John Rodgers
3.9 Jim Braash
3.9 Jim Braash
3.5 Kathy Calderon
3.2 Steve Hernandez
2.4 Stacy Lu
2.8 Faith Simmons
尽管有 7 行,但由于重复了一个,因此仍然只有 6 个学生。我已经equals()
在我的班级中实现了该方法Student
,但我无法弄清楚如何跳过该main()
方法中的行并且仍然获得与以前相同的结果。谢谢。
解决方案
使用HashSet<Student>
代替Student[]
和覆盖hascode
以符合您的equals
. 您将不再有任何重复项。
请注意,错误实现equals
和可能会导致严重问题hashcode
。不应修改此方法中使用的属性。这将导致可能的重复和/或您可能无法访问或删除HashSet
.
推荐阅读
- dataweave - 在 mule 4 中使用选择进行条件检查
- php - 尝试订阅现有事件时出现 Symfony 异常
- dataframe - 基于dataiku中的主键时间合并行
- sql - 访问联合查询
- ruby-on-rails - rails 6中asset_pack_path和image_pack_tag有什么区别?
- flutter - 如何使用带有迭代器的 ListView
- svn - 我可以恢复失败的 SVN 导出吗?
- entity-framework - 编辑数据库上下文以从相关实体获取名称
- javascript - 有没有办法用 javascript 从 XML 创建两个 html 表?
- visual-studio - F9 button doesnt sets breakpoint, but navigating to some website