Answer the question
In order to leave comments, you need to log in
HashSet List of students?
I can't figure out the assignment: You are developing a student registration system. For each student, you need to store the following data - full name, group number, student ID number. The unique identifier is the student ID number. The user enters student data in an endless loop until the "end" command is entered. At the end of the input, the program should display a list of students. The data structure where students are stored must discard input from the same student more than once. Keep in mind that people's names may be the same, but document numbers may not.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Student> listOfStudents = new HashSet<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Введите информацию о студенте: \"ФИО, номер группы, номер студенческого билета\"");
while (true) {
String input = scanner.nextLine();
System.out.println("Введите информацию о студенте (для завершения работы программы введите \"end\")");
String[] parts = input.split(", ");
String name = parts[0];
String group = parts[1];
String studentId = parts[2];
Student stud = new Student(name, group, studentId);
listOfStudents.add(stud);
System.out.println(listOfStudents);
if ("end".equals(input)) {
System.out.println("Список студентов:");
Iterator<Student> i = listOfStudents.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
}
}
import java.util.Objects;
public class Student {
protected String name;
protected String group;
protected String studentId;
public Student(String name, String group, String studentId) {
this.name = name;
this.group = group;
this.studentId = studentId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(name, student.name) && Objects.equals(group, student.group) &&
Objects.equals(studentId, student.studentId);
}
@Override
public int hashCode() {
return Objects.hash(name, group, studentId);
}
}
Answer the question
In order to leave comments, you need to log in
Think about what happens to the "end" line here:
String[] parts = input.split(", ");
String name = parts[0];
String group = parts[1];
String studentId = parts[2];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question