[Bluej-discuss] Chapter 4 Exercise 4.33

David J. Barnes d.j.barnes at kent.ac.uk
Mon Jul 10 10:48:29 BST 2006


Hi Marie,

The fact is that I didn't spot the extra semicolon, for which I apologise! From the description of your problem I assumed that you had misunderstood something about return values from methods.

The statement I wrote using theOneIWant was not intended to be written inside the findProduct method, but as a local call from somewhere else within the class. I was not sure of the context in which you were calling findProduct.

I am sorry if this confused you, and I am glad that three other people spotted the right solution. As Ian remarked to me, you could tell that there were lots of people finding ways to fill in time on a Friday afternoon!

All the best,

David

On Sun, 09 Jul 2006 12:32:15 +0100
"Marie Buchan" <mariebuchan at hotmail.com> wrote:

> Hi David. I'm really interested in your answer because I had 3 other replies and they all pointed out that I had added an un-necessary semi colon to the line where the if statement is. When I removed it the code worked.
> 
> I  am interested in this line of code you have given me.
> 
> public Product findProduct(int id)
> 
> Product theOneIWant = findProduct(36);
> 
> The method is called findProduct and you have suggested using "findProduct" in the body of the method. I haven't had time to experiment with it yet but would like to know if this is correct I can call findProduct within itself.
> 
> Marie
> ------------------------------------------------
> From:  David J. Barnes <d.j.barnes at kent.ac.uk>
> Reply-To:  General discussion for users of BlueJ <bluej-discuss at bluej.org>
> To:  bluej-discuss at bluej.org
> Subject:  Re: [Bluej-discuss] Chapter 4 Exercise 4.33
> Date:  Fri, 7 Jul 2006 15:28:46 +0100
> >Hi Marie,
> >
> >I suspect that this is because you have not made the link between what this method is searching for and what it returns. From the description of your problem it sounds like you are looking at the stock list for the result, which is not the right place.
> >
> >The header of the method says that findProduct will return a single Product object (or null). So when you call findProduct you will need to store what is returned somewhere, for instance:
> >
> >Product theOneIWant = findProduct(36);
> >
> >It is what is stored in the variable theOneIWant that you should then examine to see whether one was found or not. If it is null then no matching Product was found, otherwise you have it.
> >
> >Hope that helps,
> >
> >David
> >
> >On Fri, 07 Jul 2006 15:07:49 +0100
> >"Marie Buchan" <mariebuchan at hotmail.com> wrote:
> >
> > > Hi.
> > > Can anyone see why this code does not find a product? If an invalid id number is put in, the products which are there are returned instead of null being returned. If a valid id is searched for, it returns all the products.? Thanks again.
> > > Marie
> > >
> > >
> > > public Product findProduct(int id)
> > >
> > >     {
> > >
> > >          Iterator it = stock.iterator();
> > >
> > >          while(it.hasNext()){
> > >
> > >          Product product = (Product)it.next();
> > >
> > >          if(product.getID() == id);{
> > >
> > >          return product;
> > >
> > >      }
> > >
> > >  }
> > >
> > >           return null;
> > >
> > >     }
> >_______________________________________________
> >mailing list bluej-discuss at bluej.org
> >To unsubscribe or change your preferences, go to
> >http://lists.bluej.org/mailman/listinfo/bluej-discuss


More information about the bluej-discuss mailing list