Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help with ArrayList loop

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with ArrayList loop

    Hey everyone, I've been stuck on this problem for a long time. I can't seem to get my last method to return anything. It's supposed to add all of the unsold lots of an auction (if the highestBid == null) to an ArrayList, then at the end of the method it's supposed to return that list. My teacher told me to look closely at the header for the last for-each loop, but I still can't figure out what's wrong with it. Any help is much appreciated.


    import java.util.ArrayList;
     
    /**
     * A simple model of an auction.
     * The auction maintains a list of lots of arbitrary length.
     *
     * @author David J. Barnes and Michael Kolling.
     * @version 2008.03.30
     */
    public class Auction
    {
        // The list of Lots in this auction.
        private ArrayList<Lot> lots;
        // The number that will be given to the next lot entered
        // into this auction.
        private int nextLotNumber;
        //Edit
        //Matthew Slagle, June 3, 2012
        //The list of unsold lots in this auction.
        private ArrayList<Lot> getUnsold;
     
        /**
         * Create a new auction.
         */
        public Auction()
        {
            lots = new ArrayList<Lot>();
            nextLotNumber = 1;
        }
     
        /**
         * Enter a new lot into the auction.
         * @param description A description of the lot.
         */
        public void enterLot(String description)
        {
            lots.add(new Lot(nextLotNumber, description));
            nextLotNumber++;
        }
     
        /**
         * Show the full list of lots in this auction.
         */
        public void showLots()
        {
            for(Lot lot : lots) {
                System.out.println(lot.toString());
            }
        }
     
        /**
         * Bid for a lot.
         * A message indicating whether the bid is successful or not
         * is printed.
         * @param number The lot number being bid for.
         * @param bidder The person bidding for the lot.
         * @param value  The value of the bid.
         */
        public void bidFor(int lotNumber, Person bidder, long value)
        {
            Lot selectedLot = getLot(lotNumber);
            if(selectedLot != null) {
                boolean successful = selectedLot.bidFor(new Bid(bidder, value));
                if(successful) {
                    System.out.println("The bid for lot number " +
                                       lotNumber + " was successful.");
                }
                else {
                    // Report which bid is higher.
                    Bid highestBid = selectedLot.getHighestBid();
                    System.out.println("Lot number: " + lotNumber +
                                       " already has a bid of: " +
                                       highestBid.getValue());
                }
            }
        }
     
        /**
         * Return the lot with the given number. Return null
         * if a lot with this number does not exist.
         * @param lotNumber The number of the lot to return.
         */
        public Lot getLot(int lotNumber)
        {
            if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
                // The number seems to be reasonable.
                Lot selectedLot = lots.get(lotNumber - 1);
                // Include a confidence check to be sure we have the
                // right lot.
                if(selectedLot.getNumber() != lotNumber) {
                    System.out.println("Internal error: Lot number " +
                                       selectedLot.getNumber() +
                                       " was returned instead of " +
                                       lotNumber);
                    // Don't return an invalid lot.
                    selectedLot = null;
                }
                return selectedLot;
            }
            else {
                System.out.println("Lot number: " + lotNumber +
                                   " does not exist.");
                return null;
            }
        }
     
        /**
         * Ex. 4.28
         * Matthew Slagle
         * May 28, 2012
         * Prints out a list of all lots and their info.
         */
        public void close()
        {
            for(Lot lot : lots)
            {
                int number = lot.getNumber();
                System.out.println("Lot Number: " + number);
     
                String lotName = lot.getDescription();
                System.out.println(lotName);
     
                Bid highestBid = lot.getHighestBid();
                if (highestBid != null)
                {
                    System.out.println("Winner: " + highestBid.getBidder().getName());
                    System.out.println("Highest Bid: " + highestBid.getValue());
                    System.out.println();
                }
                else
                {
                    System.out.println("This lot has not been sold.");
                }
            }
        }
     
      /**
         * Ex 4.29
         * Matthew Slagle
         * May 28, 2012
         * Displays the unsold lots
         * If no lots are unsold, it lets the user know.
         */
        public ArrayList<Lot> getUnsold()
        {
          ArrayList<Lot> unsoldLots = new ArrayList<Lot>();
          for(Lot lot : unsoldLots)
          {
              int number = lot.getNumber();
              String lotName = lot.getDescription();
              Bid highestBid = lot.getHighestBid();
     
              if (highestBid == null)
              {
                  unsoldLots.add(new Lot(number, lotName));
                }
                nextLotNumber++;
            }
            System.out.println(unsoldLots);
            return unsoldLots;
        }
     
     
    }
    Last edited by mattslagle44; June 7th, 2012 at 09:44 AM. Reason: wrapped code in code tags


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with ArrayList loop

    Hello mattslagle44!
    There are some classes missing so it's difficult to say what's going on with your code but there is a problem with your logic in your getUnsold() method.
    ArrayList<Lot> unsoldLots = new ArrayList<Lot>();
    for(Lot lot : unsoldLots){ 
        // ...
    }
    In the above piece of code you create an new ArrayList which is -by default- empty. And then you run through it to do something. Can you see the problem now?
    And please edit your post and wrap your code in code tags.
    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    mattslagle44 (June 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ArrayList loop

    I see that now, thanks. I set unsoldLots to equal a new list, looked for values in the empty list, then returned the empty list.

  5. #4
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Need help with ArrayList loop

    Yep. Instead just use your lots ArrayList rather than creating a new object.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM

Tags for this Thread