Reputation: 21
I need to write a method that removes students from the ArrayList (myRoster) by student ID. If the student ID doesn't exist, the method should print an error message indicating that it is not found. I have written a remove method where I'm able to remove item by index. The error message 'Student with ID 3 was not found' is returning 6 times (3 from first remove and 3 from second error message). But I want to get one error message for second remove method which I'm calling in main method. A little help would be much appreciated.
Student Class
public class Student {
private int StudentID;
private String FirstName;
private String LastName;
private String Email;
private int age;
private int[] Grades;
//Constructor
public Student(int S_ID,String fName,String lName,String email,int Age,
int[] grade){
setStudentID(S_ID);
setFirstName(fName);
setLastName(lName);
setEmail(email);
setAge(Age);
setGrade(grade);
}
//Accessor Methods (get methods)
public int getStudentID(){
return StudentID;
}
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getEmail(){
return Email;
}
public int getAge(){
return age;
}
public int[] getGrades(){
return Grades;
}
//Mutator methods (set methods)
public void setStudentID(int StudentID){
this.StudentID=StudentID;
}
public void setFirstName(String FirstName){
this.FirstName=FirstName;
}
public void setLastName(String LastName){
this.LastName=LastName;
}
public void setEmail(String Email){
this.Email=Email;
}
public void setAge(int age){
this.age=age;
}
public void setGrade(int Grade[]){
this.Grades=Grade;
}
}
Roster Class
import java.util.ArrayList;
public class Roster {
private static ArrayList<Student> myRoster= new ArrayList<>();
public static void main(String[] args) {
add(1,"John", "Smith", "[email protected]", 20, 88,79, 59);
add(2,"Suzan", "Erickson", "Erickson_1990@gmailcom",19,91,72,85);
add(3,"Jack","Napoli","The_lawyer99yahoo.com",19,85,84,87);
add(4,"Erin", "Black","[email protected]",22,91,98,82 );
add(5,"Henry","Adam","[email protected]",25,85,84,79);
remove(3);
remove(3);//expected: This should print a message saying such a student with this ID was not found
}
public static void add(int S_ID,String fName,String lName,String email,int
Age, int grade1, int grade2, int grade3){
int[] Grades={grade1, grade2,grade3};
Student newStudent= new Student(S_ID, fName, lName, email, Age, Grades);
myRoster.add(newStudent);
}
public static void remove(int StudentID){
for (int i = 0; i < myRoster.size(); i++){
if(i == StudentID){
myRoster.remove(i);
}else{
System.out.println("Student with ID "+StudentID+" was not found");
}
}
}
}
}
Upvotes: 1
Views: 3208
Reputation: 2578
Yes you can do this ,please some add things in your Student class
public class Student {
private int StudentID;
private String FirstName;
private String LastName;
private String Email;
private int age;
private int[] Grades;
public Student(int S_ID ){
this.setStudentID(S_ID);
}
//Constructor
public Student(int S_ID,String fName,String lName,String email,int Age,
int[] grade){
setStudentID(S_ID);
setFirstName(fName);
setLastName(lName);
setEmail(email);
setAge(Age);
setGrade(grade);
}
//Accessor Methods (get methods)
public int getStudentID(){
return StudentID;
}
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getEmail(){
return Email;
}
public int getAge(){
return age;
}
public int[] getGrades(){
return Grades;
}
//Mutator methods (set methods)
public void setStudentID(int StudentID){
this.StudentID=StudentID;
}
public void setFirstName(String FirstName){
this.FirstName=FirstName;
}
public void setLastName(String LastName){
this.LastName=LastName;
}
public void setEmail(String Email){
this.Email=Email;
}
public void setAge(int age){
this.age=age;
}
public void setGrade(int Grade[]){
this.Grades=Grade;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return StudentID == student.StudentID;
}
@Override
public int hashCode() {
return StudentID;
}
}
and in main area class you can do
public class Test {
public static void main(String []args){
List<Student> list = new ArrayList<>();
list.add(new Student(1,"a","a","a",1, new int[]{1}));
list.add(new Student(2,"b","b","b",2, new int[]{2}));
list.remove(new Student(1));
list.forEach(System.out::println);
}
}
Output
com.traveliko.platform.web.frontend.Student@2
Upvotes: 0
Reputation: 56469
i
with the parameter StudentID
rather it should be if(myRoster.get(i).getStudentID == StudentID)
.The reasoning as to why the text "Student with ID "+StudentID+" was not found" is being printed to the console multiple times is because you've inserted it inside the loop, meaning each time the parameter StudentID
doesn't match the value that it's being compared to, it will print the same message...
To accomplish your task you can simply use the ArrayList#removeIf
method to remove the Student
with the specified StudentID
, else if the ArrayList#removeIf
returns false then you can print the appropriate message as shown within the solution below.
public static void remove(int StudentID) {
if (!myRoster.removeIf(s -> s.getStudentId() == StudentID))
System.out.println("Student with ID " + StudentID + " was not found");
}
Upvotes: 2
Reputation: 2181
You can use Iterator interface as well for your purpose as shown below.
public static void remove(int StudentID) {
Iterator<Student> it = myRoster.iterator();
while(it.hasNext()) {
Student stud = (Student)it.next();
if (stud.getStudentID() == StudentID) {
it.remove();
return;
}
}
System.out.println("Student with ID " + StudentID + " was not found");
}
Upvotes: 0
Reputation: 681
You problem seems to be that your loop in the remove() method isn't looping over studentIDs but just the looping index. You should loop over studentIDs. Your code prints each time an ID doesn't match the given ID. That's why you get multiple print messages. You should instead only print if no one matches (i.e. roster size doesn't change).
public static void remove(int StudentID){
int initialSize = myRoster.size(); //store initial size of roster to check if it changes
for (int i=0;i<myRoster.size();i++){
if(myRoster.get(i).getStudentID == StudentID){
myRoster.remove(i);
}
}
//checks if a student was removed which means the ID was found.
if(initialSize == myRoster.size()){
System.out.println("Student with ID "+StudentID+" was not found");
}
}
Upvotes: 0
Reputation: 80
Your remove method is currently giving the error message once for every student in the list that doesn't have the desired student ID.
for (int i=0;i<myRoster.size();i++){
if(i==StudentID){
myRoster.remove(i);
}else{
System.out.println("Student with ID "+StudentID+" was not found"); }
That print statement is called any time the student at index i
does not have the desired ID, not when there are no students that have that ID.
An easy way to fix this would be to put a boolean
at the start of the method and set it to false. Then, in the if (i == StudentID)
block, remove the student and set the value to true. Then, after the loop is complete, check if the boolean is true; if it is true, then the desired student has been removed. If it is still false, then there is no student with the desired ID.
It should look something like this:
public static void remove(int studentID) {
boolean studentFound = false;
for (int i=0;i<myRoster.size();i++){
if(i==StudentID){
myRoster.remove(i);
i--; // you have to decrement this value to account for the missing item
studentFound = true; // student with desired ID is removed
}
if (!studentFound) System.out.println("Student with ID "+StudentID+" was not found");
}
Upvotes: 0