• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assistance with the Remove method  RSS feed

 
John Hartline
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody! I was wondering if someone could point me in the right direction. I'm working with an array list and would like to remove one of the objects but I'm getting an error stating that the symbol cannot be found. I'm pretty confident I have called the variable as I should so I don't know where to go on this one. I'm a very beginner programmer and this is for a school assessment, so don't shout out the answer but I would appreciate a little light on the subject. I'm using NetBeans to compile so that platform is what is telling me the errors.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in main() have you defined a variable named "studentID" ?

Also, in remove(), since you haven't shown us what the Student class looks like, we can only guess.
Does the Student class contain a method called "remove" that takes a String as its single argument?

And in remove(), you are trying to compare a String variable (ident)  to a Student variable (s).

The entire remove() method in Roster seems very suspect. What is it supposed to do?
 
John Hartline
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That variable came from another class; another part of the project. The code for that is below. As I understand it, under this Roster class, it should be able to find the studentID with the getter methods.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Student does not have a remove() method, so does not make any sense.
Presumably what you want to do is remove a particular Student from the studentList ArrayList, which is not done by invoking any method of Student.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, your remove() method takes the ID of the student to remove from the list. therefore you should search the list to find a Student with the matching ID, and then remove that Student from the ArrayList. You should then quit searching the rest of the list, since you have already accomplished the task.

This would be much easier if you used a Map of some sort, with the key being the studentID and the mapped object being the Student (but you probably haven't learned Maps yet)

In Roster's add() method, what should happen if there is already a Student in the ArrayList whose studentID is the same as the studentID passed in to the add() method?
 
John Hartline
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm wondering if I'm even in the ballpark with this method. It sounds like instead of

for(Student s : studentList) {
            String ident = s.getStudentID();

to get a list of all student ID's, I need some search function to pass ("3") into as a parameter to search for. Is that what you mean?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing should work - just add the removal statement

Note that for completeness, you should handle the possibility that studentID is null before executing the if-test.
Note that the above code assumes that the list is not allowed to have multiple entries with the same studentID.
        
 
John Hartline
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I see what you did and it makes sense. I'll paste the portion of the rubric below to show the objective I'm shooting for, maybe that will clear up what I'm after:
3.  Create a student Roster class with the following methods that contain all ArrayList method calls:
a.  public static void remove(String studentID) that removes students from the roster by student ID

Note: If the student ID doesn’t exist, the method should print an error message indicating that it is not found.

Now, I've implemented what you suggested and it accomplishes the removal of a student. When I send remove("3"); in the Main method, it removes the student associated with studentID("3"). Mission accomplished. Your note is on track with what I'm looking for next. If the studentID doesn't exist, I need to print a message. I was thinking something along the lines like:
if (studentID exists) {   // Not sure how to implement that
            System.out.println(ident + " is in the system");
            }
            else {
            System.out.println(ident + " does not exist.");
            }
I was hoping to be able to use a boolean check but it won't cooperate since the studentID is a string. I'm going to play with it and see if I can cast it into the appropriate type. Thanks for the assistance so far, I greatly appreciate it.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one way to proceed is this:
Instead of the break statement, just use a return statement, since at that point the function has done all of it job.
Then after the loop ends you can write a message saying that the desired Student was not found in the list. No test is necessary here, since the statements after the end of the loop will not be executed because of the return statement executed if the student was found while traversing the loop.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is to read the documentation for the remove() method and understand what the returned value tells you. If you do that, you'll realize that most of the work is already done for you.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we are on about your code, why does the keyword static appear so frequently? I recommend you remove it everywhere (except before the main method. I would recommend you put the main method in another class.All the code at present in the main method shou‍ld go in the run method. See if you can't find a better name for that method. You will not be able to use the add method in a static style; the easiest way to change it would be to writeCode copied from your earlier post. My, that is well formatted code
Consider whether the add method is the best way to add a Student; at present it is tightly coupled to the implementation of the Student class. Maybe you shou‍ld change it to add(Student s)

You don't need to write the three parameters separately: change the parameters to read double... grades and the JVM will create the array automatically for you. That is called varargs which is short for variable arity arguments. Try here in the Java™ Tutorials under “Arbitrary Number of Arguments”.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Another way is to read the documentation for the remove() method and understand what the returned value tells you. If you do that, you'll realize that most of the work is already done for you.

Being able to find and read the Java documentation is essential for Java programming.  I would Google java 8 arraylist or bookmark https://docs.oracle.com/javase/8/docs/api/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!