array duplication check

Asked 2 years ago, Updated 2 years ago, 335 views

We have created a program that checks for duplicate registrants and asks for the average age, and the current situation is as follows:The average age is required, but it doesn't seem to be able to check for duplication.

public class Main {
    
    public void main(String[]args) { 
        StudentInfo student=new StudentInfo(); 
        student.addStudents(newStudents("Taro Sato", "111", 21); 
        student.addStudents(newStudents("Taro Sato", "111", 21); 
        student.addStudents(new Student("Akari Kato", "112", 20); 
        student.addStudents(newStudents("Taizo Hayashi", "113", 23); 
        student.addStudents(new Student("Hikari Watanabe", "114", 22); 
                
        double average=StudentInfo.getAverage(); 
        System.out.println("average age:" + average); 
        
    }
}

public class Student {

    String name="; // Name    
    String studentID=";// Student ID    
    age;// age 
    public Object id;

    public Student(String name, String id, intage) {        
        this.name = name;        
        This.studentID=id;        
        This.age=age;
    }
    
    public String getName() {
        return name;
    }
    public String getId() {
        return studentID;
    }
    public int getage() {
        return;
    }
}

public class StudentInfo{
    
    public static final int MAX_COUNT = 100; // Maximum number of Student handled;
    static Student [ ] students = new Student [MAX_COUNT ]; // Student to manage
    static int studentCount = 0; // Number of Students stored in the array

    public String id="; // Initialization
    public String name="";
    public age = 0;

    // Argument: Student/Student Add Student
    // ·Return value: Returns true when added and false when failed.
    // ·Purpose: Add Student to the array in StudentInfo. If the value of the argument is null,
    //         If you have exceeded the maximum number that you can handle in an array, or if you have already registered a Student with the same information,
    //         return false without registering

    public boolean addStudents (Student student) {
        // Returns false if argument specifies a class other than Student
        if(student instance of Student!=true)
                return false;
        // When the maximum number of arrays is exceeded
        if(studentCount>MAX_COUNT||student==null){
                return false;
            }
        // Check if each element in the array is the same and return true if they are all the same
            Student target=(Student) student;
            // Duplicate check with for statement
            for(inti=0;i<=studentCount;i++){
            
            if(this.name.equals(target.name)&(this.id.equals(target.id))&this.age==target.age)
                return false;
            }
                Students [studentCount] = student; /// student [studentCount] replaces student
                studentCount++;
                System.out.println(studentCount); 
            
                return true;                
            }
    
    // · Argument: None
    // ·Return value: Average Student value registered in the array
    // Objective: Calculate and return the average age of Student registered in the StudentInfo array.
    //         Returns 0 if no student is registered.

    public static double getAverage() {
        double average = 0.0;
            for (inti=0;i<studentCount;i++)
                average+=(double)students[i].getage();
                System.out.println (average/studentCount); 
            return average/studentCount;
    }

java array

2022-09-30 21:50

2 Answers

The Student.id variable has not been initialized, so it should remain null.
Therefore,

this.id.equals(...)

It appears to be NullPointerException in the method call.

id I think that if you initialize the member properly, the event will be resolved.(*I couldn't predict the value from the code example.)

Note that string comparisons are correct using the equals method instead of the == operator.


2022-09-30 21:50

I think there are a few things that need to be fixed.

"·In order to ""confirm that each element of the array is the same"", it is necessary to check the elements in the array repeatedly (for)."

Student target=(Student)student;
for (Students:students) {
    this.name=s.getName();
    this.id=s.getId();
    This.age=s.getAge();
    if(this.name.equals(target.getName())||(this.id.equals(target.getId))|||(this.age==target.getAge())){
        return false;
    }
}
studentCount++;
return true;

·As the data was substituted into the array before the duplicate check was performed, it is expected that the data of other students will be overwritten even if it is duplicated.

students[studentCount] = student;

Why don't you move the above action after a duplicate check?

·In the StudentInfo class, the id is defined in the Object class, but if you look at the Student class, it seems that the id is String.

private String id="";

Why don't you change it to ?


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.