Results 1 to 2 of 2
  1. #1
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    24
    Rep Power
    0

    Default how to make the right return message

    Hello,
    Still struggling whith java in bluejbook (Barnes/Kolling), now question 4.52.
    I must search an object (Lot) in an ArrayList (lots) with a number which is a field in that object. Once found, the Lot must be removed. So far so good, but it must also be returned. And when it is not found al null must be returned. I used an Iterator but my code only returns the object when I remove the last. I can see why that is when I look at my code, but I don't know how to solve it. Has anybody got a suggestion what to do?
    The code:

    Java Code:
    public Lot removeLot(int number)
        {
            Iterator<Lot> it = lots.iterator();
            Lot tempLot = null;
            while (it.hasNext())
            
            {
                tempLot = it.next();
                if (tempLot.getNumber()==number)
                {
                    it.remove();
                }
                else
                {
                    tempLot=null;
                }
                
               
               
            }
            return tempLot;
        }
    Last edited by Maarten; 11-27-2013 at 08:40 PM.

  2. #2
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    24
    Rep Power
    0

    Default Re: how to make the right return message

    Aha I found it. I added
    Java Code:
     return tempLot
    just under the
    Java Code:
    it.remove();
    part and I removed the else part. cheers.

Similar Threads

  1. Replies: 3
    Last Post: 10-26-2013, 01:06 PM
  2. how to return value in message driven bean?
    By jimmy6 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-28-2013, 10:48 AM
  3. How to return command prompt error message
    By raxwer in forum Advanced Java
    Replies: 3
    Last Post: 05-26-2010, 05:15 PM
  4. Trying to make a return....
    By Torgero in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 07:47 PM
  5. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •