Page 1 of 2 12 LastLast
Results 1 to 20 of 40
  1. #1
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default ArrayList find values

    I have a method that is intended to find a name and an int value belonging to that name.
    It is about an auction system where people can bid on an item.
    when the auction is closed then there is a method see belove
    The task have class person where only name is stored
    class bid where name of the bidder and the value of the bid are stored
    class lot store a number of the item/lot the highestbid and description of the item
    class auction an arraylist containing info about everything
    Java Code:
    Public void close()
    {
       Iterator it = lots.iterator(); //iterate trough the lots arraylist where info about items, name of                     
            while(it.hasNext()) {                                //bidder and the value of the bid is stored
                Lot lot = (Lot) it.next();
                if(lot.getNumber() != null); //if the number of the lot/item has any value
            {
                System.out.println(person.getName() + " " + bid.getValue()); //print out name
                                                                                                  //and the bid/value
    
               
    //            System.out.println(lot.toString());
            }
        }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    And what is your problem now? That this code will print all lots ? :)
    if(lot.getNumber() != null); <-- remove the last character ^^

  3. #3
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Here is the method now, and it stil don't print out anything
    Java Code:
    public void close()
    {
        Iterator it = lots.iterator();
            while(it.hasNext()) {
                 
                   Person person = (Person) it.next();  //name
                   Lot lot = (Lot) it.next();     //number, description, gethighestbid
                   Bid bid       = (Bid) it.next();     //bidder, value
                   if(lot  != null)
                {
                   System.out.println(lot.getName() + " " + bid.getValue()); 
            }
            }
    
    This is the error code:
     java.lang.ClassCastExeption
    Lot cannot be cast to Person

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    How is the variable lots defined?
    If a container what does it contain?
    The jvm error says this: Lot cannot be cast to Person
    Does it contain Lot objects?

    For debugging change your code:
    Person person = (Person) it.next();
    to
    Object obj = it.next();
    System.out.println("obj=" + obj); // Show what type it.next() is returning.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    Lot cannot be cast to Person
    Good! You would certainly have a smelly design if Lot is-a Person.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I just noticed. Every it.next() is being cast to a different type? How can that be? What does lots have in it?
    Java Code:
                   Person person = (Person) it.next();  //name
                   Lot lot           = (Lot) it.next();     //number, description, gethighestbid
                   Bid bid          = (Bid) it.next();     //bidder, value

  7. #7
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    The error code now:
    java.utilNoSuchElementException:
    null(in java.util.AbstractList$Itr)

    Lots should have the lot/item, name of the bidder and the value of the bid

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Please copy and paste here the FULL text of the error message. You are leaving off some important things.

    Its probably the fact that you are only testing hasNext() one time and then are getting > 1 items with next()
    You must test before each next()

  9. #9
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    The full text:

    java.util.NoSuchElementException
    at java.util.AbstractList$Itr.next(AbstractList.java: 350)
    at Auction.close(Auction.java:123)

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Java Code:
    while(it.hasNext()) {
        Person person = (Person) it.next();
        Lot lot = (Lot) it.next();
        Bid bid       = (Bid) it.next();
    Ignoring all the casting problems. The hasNext check is only performed once. So what happens if there is only one item left in the Iterator and you call next three times? If the information is stored inside the Lot item then shouldn't you be getting the next Lot element from the Iterator once each time around the loop and then extracting the rest of the information from that Lot object.

  11. #11
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    How do I do that?

    I am a newbie

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    How do you do what? Call methods? If you don't know how to do that then you need to go back to basics.

  13. #13
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    With the code belove I am able to retrive the value from the bid, but if I try to to retrive the name with "person.getName()" it won't work
    Java Code:
       Iterator it = lots.iterator();
            while(it.hasNext()) {
                      
                        Lot lot       = (Lot) it.next(); //number, description, gethighestbid
                        if(lot  != null)
                        {
                             System.out.println(lot.getHighestBid().getValue());
                        }

  14. #14
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    With the code below I am able to extract the value from the bid, but if I try to to extract the name with "lot.getName()" it won't work
    Code:
    Iterator it = lots.iterator();
    while(it.hasNext()) {

    Lot lot = (Lot) it.next(); //number, description, gethighestbid
    if(lot != null)
    {
    System.out.println(lot.getHighestBid().getValue()) ;

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Back in post#4 I asked several questions that you have not answered.
    If you need further help, try to answer them. If you are ok with your code, then good.

    How is the variable lots defined?
    If a container what does it contain?

    What is the relationship,if any, between the classes: Lot, Person and Bid?

  16. #16
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Java Code:
    Object obj = it.next();
    System.out.println("obj=" + obj); // Show what type it.next() is returning.
    
    Here is the error code when using the lines above
    
    java.util.NoSuchElementException
    	at java.util.AbstractList$Itr.next(AbstractList.java:350)
    	at Auction.close(Auction.java:123)
    
    
    
    Here you can see the class Lot and class person
     
    
    /**
     * A class to model an item (or set of items) in an
     * auction: a lot.
     * 
     */
    public class Lot
    {
        // A unique identifying number.
        private final int number;
        // A description of the lot.
        private String description;
        // The current highest bid for this lot.
        private Bid highestBid;
    
        /**
         * Construct a Lot, setting its number and description.
         * @param number The lot number.
         * @param description A description of this lot.
         */
        public Lot(int number, String description)
        {
            this.number = number;
            this.description = description;
        }
    
        /**
         * Attempt to bid for this lot. A successful bid
         * must have a value higher than any existing bid.
         * @param bid A new bid.
         * @return true if successful, false otherwise
         */
        public boolean bidFor(Bid bid)
        {
            if((highestBid == null) ||
                   (bid.getValue() > highestBid.getValue())) {
                // This bid is the best so far.
                highestBid = bid;
                return true;
            }
            else {
                return false;
            }
        }
        
        /**
         * @return A string representation of this lot's details.
         */
        public String toString()
        {
            String details = number + ": " + description;
            if(highestBid != null) {
                details += "    Bid: " + 
                           highestBid.getValue();
            }
            else {
                details += "    (No bid)";
            }
            return details;
        }
    
        /**
         * @return The lot's number.
         */
        public int getNumber()
        {
            return number;
        }
    
        /**
         * @return The lot's description.
         */
        public String getDescription()
        {
            return description;
        }
    
        /**
         * @return The highest bid for this lot. This could be null if
         * there are no current bids.
         */
        public Bid getHighestBid()
        {
            return highestBid;
        }
    }
    /////////////////////////////////
    /**
     * Maintain details of someone who participates in an auction.
     */
    public class Person
    {
        // The name of this user.
        private final String name;
    
        /**
         * Create a new user with the given name.
         * @param name The user's name.
         */
        public Person(String name)
        {
            this.name = name;
        }
    
        /**
         * @return The user's name.
         */
        public String getName()
        {
            return name;
        }
    }
    ///////////////////////////////
    The field in class Bid
    public class Bid
    {
        // The user making the bid.
        private final Person bidder;
        // The value of the bid. This could be a large number so
        // the long type has been used.
        private final long value;
    //////////////////////////////////////////////////////////////
    class Lot
    /**
     * A class to model an item (or set of items) in an
     * auction: a lot.
     * 
     */
    public class Lot
    {
        // A unique identifying number.
        private final int number;
        // A description of the lot.
        private String description;
        // The current highest bid for this lot.
        private Bid highestBid;
    
        /**
         * Construct a Lot, setting its number and description.
         * @param number The lot number.
         * @param description A description of this lot.
         */
        public Lot(int number, String description)
        {
            this.number = number;
            this.description = description;
        }
    
        /**
         * Attempt to bid for this lot. A successful bid
         * must have a value higher than any existing bid.
         * @param bid A new bid.
         * @return true if successful, false otherwise
         */
        public boolean bidFor(Bid bid)
        {
            if((highestBid == null) ||
                   (bid.getValue() > highestBid.getValue())) {
                // This bid is the best so far.
                highestBid = bid;
                return true;
            }
            else {
                return false;
            }
        }
        
        /**
         * @return A string representation of this lot's details.
         */
        public String toString()
        {
            String details = number + ": " + description;
            if(highestBid != null) {
                details += "    Bid: " + 
                           highestBid.getValue();
            }
            else {
                details += "    (No bid)";
            }
            return details;
        }
    
        /**
         * @return The lot's number.
         */
        public int getNumber()
        {
            return number;
        }
    
        /**
         * @return The lot's description.
         */
        public String getDescription()
        {
            return description;
        }
    
        /**
         * @return The highest bid for this lot. This could be null if
         * there are no current bids.
         */
        public Bid getHighestBid()
        {
            return highestBid;
        }
    }

  17. #17
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I have solved it:
    Java Code:
     System.out.println("This is the winner bid: " + lot.getHighestBid().getValue() + ": " + "This is the name of the bidder"+ lot.getHighestBid().getBidder().getName());
    Thanks for all you help Norm!

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I guess the answer is: There is NO relationship between those 3 classes. Is that correct?

    Here is the error code when using the lines above
    That is useless unless you show the context of the lines (IE what lines of code are in front of them) and all of the printed output.


    I have solved it:
    Ok. On to the next problem

  19. #19
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello again!
    On to next problem.
    How to print out the lot which has not been sold from that same arraylist?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    How to print out the lot which has not been sold from that same arraylist?
    Loop through the list, identify those not sold and print them

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 04-28-2011, 11:25 AM
  2. Replies: 1
    Last Post: 04-14-2011, 01:48 PM
  3. Problem with keeping values into an arrayList
    By Daniel Silvester in forum New To Java
    Replies: 0
    Last Post: 12-02-2009, 01:14 AM
  4. Replies: 5
    Last Post: 07-28-2009, 10:22 PM
  5. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM

Posting Permissions

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