0

I have been working on this for hours so I hope someone can help me. I have to create an arraylist of students and do the following commands. add, find and delete students. Anyways my add, find functions work ok, but when I try to delete students it brings up the wrong student! I dont' know what to do feels like I have tried everything.

public void addStudent(String studentName, long studentID, String address) {
    Student newStudent = new Student ( studentName,  studentID, address);
    collegeList.add(newStudent);
}

public static  void deleteStudent() {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter student ID");
    long studentNumber=input.nextLong();

    if(directory.isValidID(studentNumber) && directory. withinRange(studentNumber)) {
        System.out.print("Deleting Student");
        System.out.print(directory.findStudent(studentNumber));
        System.out.print("please confirm with y/n");
        Scanner myans=new Scanner(System.in);
        String confirmation=myans.next();

        if (confirmation.equals("y")) {
            directory.deleteStudent(studentNumber);
            System.out.print("student deleted");
        }

        if(confirmation.equals("n")) {
            System.exit(0);
        }
    }
}

/**
Searches for student based upon their student number
@param studentID unique student number for each student
@return students entire information
*/
public String findStudent(long studentID) { 
    String str;
    Student newStu;

    for (int i=0; i<collegeList.size(); i++ ) {
        newStu=collegeList.get(i);

        if(newStu.getStudentID()==studentID);

            return newStu.toString(); 
    }

    return null;
}

/**
Removing student from collegeList
@param studentID unique student number
@return none
*/
public void deleteStudent (long studentID) { 
    Student newStu;
    for (int i=0; i<collegeList.size(); i++ ) {
        newStu=collegeList.get(i);

        if (newStu.getStudentID()==studentID)
            collegeList.remove(i);
    }
}
6
  • Use an iterator instead of List#remove. Commented Oct 3, 2013 at 3:39
  • My findstudent method is actually incorrect it is not finding the right students... Commented Oct 3, 2013 at 3:44
  • Now that you mention it, why are you returning a String in the find method instead of the Student object? Commented Oct 3, 2013 at 3:46
  • is if(newStu.getStudentID()==studentID); return newStu.toString(); lines right? Commented Oct 3, 2013 at 3:47
  • 1
    Instead of changing the existing question completely, it is often a better idea to ask a new question instead. That makes it so answers that have already been submitted still make sense. That being said, I'm removing my answer and upvoting @Frode since he has answered your "new" question. Commented Oct 3, 2013 at 13:38

2 Answers 2

3

Please correct me if I am mistaken, but it looks like your comparison is wrong. It updates highest if you have found a lower score then highest (exact opposite of what you want).

highest>newStu.getQuizScore()

should perhaps be

highest<newStu.getQuizScore()

You also need to iterate the entire list to find the highest score. Now you return the first score that is lower then the first score, but that may not be correct. I would do something like this:

public Student findHighest () {

    Student highest;
    highest=collegeList.get(0);


    for (int i=1; i<collegeList.size(); i++ ) {

        Student newStu=collegeList.get(i);

        if (highest.getQuizScore()<newStu.getQuizScore()){
            highest=newStu;
        }

    }
    return highest;

}

Sorry for any mistakes or problems with my answer, I am new to Stack Overflow.

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

0

Does your Student class override the hashCode() and equals() methods? If not, it won't behave properly with Java collections.

Then you should be able to do things like:

studentList.add(student);
...
int index = studentList.indexOf(student);
if (index != -1) return studentList.get(index);
...
studentList.remove(student);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.