Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

iterator.

Thread view: 
ste1986 - 29 Dec 2005 18:55 GMT
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;
   }
}
VisionSet - 29 Dec 2005 19:15 GMT
> im curently doing an assignment for uni and there is somthing wrong with
> the code below but i dont know what.
[quoted text clipped - 11 lines]
>     }
> }

'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
Roedy Green - 29 Dec 2005 19:36 GMT
>// add method
>public boolean add(Volunteer volunteer)

add methods add something. Your method does no such thing, you should
not mislead with such a name.  Since it returns boolean, you might
call it

contains
canAdd
isAddable
isUndefined

flipping the sense of the boolean as necessary.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

VisionSet - 30 Dec 2005 00:02 GMT
> >// add method
> >public boolean add(Volunteer volunteer)
>
> add methods add something. Your method does no such thing, you should
> not mislead with such a name.  Since it returns boolean, you might
> call it...

I've become accustomed to you not replying properly to the poster you
intend, but it sure is annoying Roedy :-)

Why quote my method which does add something under the right conditions in
the same way Collection.add is specified.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add(E)

--
Mike W
Roedy Green - 30 Dec 2005 09:12 GMT
>> >// add method
>> >public boolean add(Volunteer volunteer)
[quoted text clipped - 5 lines]
>I've become accustomed to you not replying properly to the poster you
>intend, but it sure is annoying Roedy :-)

That is a direct quote from your post. I'm not writing email. I am
talking to anyone following the discussion. The important thing is the
code, not who wrote it.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Gordon Beaton - 30 Dec 2005 09:33 GMT
> I've become accustomed to you not replying properly to the poster
> you intend, but it sure is annoying Roedy :-)

Honestly, why the simley?

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

VisionSet - 30 Dec 2005 12:04 GMT
> > I've become accustomed to you not replying properly to the poster
> > you intend, but it sure is annoying Roedy :-)
>
> Honestly, why the simley?

Because generally I'm a stroppy so-and-so and that's my nicer side trying to
get out.

--
Mike W
Roedy Green - 29 Dec 2005 19:17 GMT
>public volunteer.add(new Volunteer(String name, int telephone))
>    {
[quoted text clipped - 8 lines]
>    }
>}

What is this supposed to be?
If a method declaration, you can't have a dot in a method name.
If this is supposed to be an anonymous class, then you don't want the
word public there.

Iterators are more neatly handled with for:each.

The contains method might do what you want if your volunteers supports
it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mark Haase - 30 Dec 2005 05:53 GMT
In article
<73094ec574c1a58aebddecfff73e2086@localhost.talkaboutprogramming.com>,

> im curently doing an assignment for uni and there is somthing wrong with
> the code below but i dont know what.
[quoted text clipped - 11 lines]
>     }
> }

You have an assignment over XMas break?

One problem is that you either have two classes with nearly the same
name (Volunteer, Volunteers), or else you've confused the two of them.
If you can't figure this out from the error code -- which you didn't
post, then you need to go to your professor's office hours.

|\/|  /|  |2  |<
mehaase(at)gmail(dot)com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.