Re: iterator.




"ste1986" <steiddon_1@xxxxxxxxxxx> wrote in message
news:73094ec574c1a58aebddecfff73e2086@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> im curently doing an assignment for uni and there is somthing wrong with
> the code below but i dont know what.
>
> public volunteer.add(new Volunteer(String name, int telephone))
> {
> Iterator it = volunteers.iterator();
> while (it.hasNext())
> {
> Volunteers volunteers = (Volunteers) it.next();
> if (volunteers.getName().equals(name))
> return true;
> }
> return false;
> }
> }
>

'Something wrong' is not helpful. What is wrong? Quote a stack traces or
compiler errors.

However try this to begin with

// create new Volunteer
Volunteer volunteer = new Volunteer(String name, int telephone);

// Call add method with new volunteer
add(volunteer);

// add method
public boolean add(Volunteer volunteer)
{
Iterator it = volunteers.iterator();
while (it.hasNext())
{
Volunteers volunteers = (Volunteers) it.next();
if (volunteers.getName().equals(volunteer.getName())) //
does existing collection contain volunteer?
return false; // false indictating can't add
}
volunteers.add(volunteer); // no volunteer of this name, so add
return true; // return true indicating success
}
}

You'll need a getName() method in your Voluteer class which I imagine you
have or should have.

--
Mike W


.



Relevant Pages

  • Iterator problem.
    ... i need a method to check if a volunteer is already in a list and if not add ... * Constructor for objects of class helpSystem ... public void addVolunteer(String name, int telephone) ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: Conventions
    ... >> I wonder if it would be possible to have a restaurant with an emotional ... Volunteer servers and cooks. ... Most people pay for the ... all day, dont spend the night. ...
    (rec.arts.sf.fandom)
  • iterator.
    ... im curently doing an assignment for uni and there is somthing wrong with ... the code below but i dont know what. ... public volunteer.add(new Volunteer(String name, int telephone)) ...
    (comp.lang.java.help)
  • Re: BJC full price registration open and other important info.
    ... If you're not able to volunteer before the show you want to see (say ... thats gonna be for the convention organisers - 500 people having volunteered ... We dont think its much to ask people to support us in our attempts to get it ... The British'ish Cabaret is a new thing added to the convention. ...
    (rec.juggling)
  • Re: Why we should assassinate foriegn leaders...
    ... I dont have to explain it to anyone else, ... so which boys do you think should jump up and volunteer? ...
    (rec.scuba)