There is a method call at the end of the main of course that calls upon a String method. My original plan was to create an array reference from Roster then only copy elements that do not have Stern in them.
I ended up just doing that, cleared .roster, and then copied the elements from one Array to another but it is still returning the same roster count of 4. At this point I am unsure how to proceed forward and will hopefully the some guidance. Do realize that the code, methods, and etc cannot be altered. The only thing that can be changed is the contents in the method. droppedStudent().
package study3;
import java.util.ArrayList;
public class Course {
private ArrayList<Student> roster; // Collection of Student objects
public Course() {
roster = new ArrayList<Student>();
}
// Drops a student from course by removing student from course roster
public void dropStudent(String last) {
ArrayList<Student> nonDropList = new ArrayList<>();
// while (!press.equals("q")) {
for (Student nondrop: roster) {
if (!nondrop.getLast().equals(last)) {
nonDropList.add(nondrop);
}
}
roster = new ArrayList(nonDropList);
}
public void addStudent(Student s) {
roster.add(s);
}
public int countStudents() {
return roster.size();
}
// main
public static void main(String args[]) {
Course course = new Course();
String first; // first name
String last; // last name
double gpa; // grade point average
int beforeCount;
first = "Henry";
last = "Nguyen";
gpa = 3.5;
course.addStudent(new Student(first, last, gpa)); // Add 1st student
first = "Brenda";
last = "Stern";
gpa = 2.0;
course.addStudent(new Student(first, last, gpa)); // Add 2nd student
first = "Lynda";
last = "Robison";
gpa = 3.2;
course.addStudent(new Student(first, last, gpa)); // Add 3rd student
first = "Sonya";
last = "King";
gpa = 3.9;
course.addStudent(new Student(first, last, gpa)); // Add 4th student
beforeCount = course.countStudents(); // Number of students before dropping any students
last = "Stern";
course.dropStudent(last); // Should drop Brenda Stern
System.out.println("Course size: " beforeCount " students"); // Should output 4
System.out.println("Course size after drop: " course.countStudents() " students"); // Should output 3
}
}
CodePudding user response:
There are two things wrong with this section of the code:
if (nondrop.getLast() != last);
nonDropList.add(nondrop);
In Java, an if
statement takes the form
if (CONDITION) SOME_CODE
If the condition in CONDITION
is true
, then SOME_CODE
is executed, otherwise SOME_CODE
is skipped over.
The first problem is the ;
at the end of the if
line. This is interpreted as the SOME_CODE
and so it is this that gets conditionally executed. A ;
on its own in a position like this is an empty statement, and executing it does nothing.
One way to fix this problem is simply to delete this semicolon. The next statement in your code, nonDropList.add(nondrop);
, will then form the body of the if
statement and hence only be executed if the condition matches. SOME_CODE
is normally the next statement after the condition, but you can put multiple statements in an if
by surrounding them with braces ({
and }
). Even though you only have one statement, I would recommend putting braces around this line to make it clear that it is what the if
applies to, and also to make it easier to add further lines in the future if you should need to. I've also indented this line to make it clear that it is 'inside' the if
statement:
if (nondrop.getLast() != last) {
nonDropList.add(nondrop);
}
The second problem is the use of !=
to compare strings. You need to use .equals()
to compare strings in Java, for the reason why see this question. There's no .notEquals()
method or similar, so you have to use .equals()
and invert the result:
if (!nondrop.getLast().equals(last)) {
nonDropList.add(nondrop);
}