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: Problem accessing specific data in an array and getting it to return properly

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem accessing specific data in an array and getting it to return properly

    Hi guys. Java is really kicking my ass. I am taking this course online with no previous language experience and unfortunately I know no one that is proficient with Java. So here I am . Any assistance would be GREATLY appreciated. I am sorry if I posted this in the wrong forum. I am a newb.

    Please let me give you the backdrop of my code.

    Implement the findProduct method. This should look through the collection for a product whoose ID field matches the ID argument of this method. Id a matching product is found it should be returned as the method's result. If no matching product is found, return null.

    When looking for a match, you will need to call the getID method on a product. This means that you will need to cast when you retrieve an item from the list.

    I tried my best at trying to get it to work properly to return the requested product. However the best I could do is not enough. It keeps returning the null even if I put in a correct ID number and it returns a product even if I put in an incorrect ID number.

    import java.util.ArrayList;
    import java.util.Iterator;
     
    /**
     * Manage the stock in a business.
     * The stock is described by zero or more Products.
     * 
     * @author Matthew Wagner 
     * @version 02 Feb 2010
     */
    public class StockManager
    {
        // A list of the products.
        private ArrayList stock;
     
        /**
         * Initialise the stock manager.
         */
        public StockManager()
        {
            stock = new ArrayList();
        }
     
        /**
         * Add a product to the list.
         * @param item The item to be added.
         */
        public void addProduct(Product item)
        {
            stock.add(item);
        }
     
        /**
         * Receive a delivery of a particular product.
         * Increase the quantity of the product by the given amount.
         * @param id The ID of the product.
         * @param amount The amount to increase the quantity by.
         */
        public void delivery(int id, int amount)
        {
        }
     
        /**
         * Try to find a product in the stock with the given id.
         * @return The identified product, or null if there is none
         *         with a matching ID.
         */
        public Product findProduct(int id)
        {
            int found = 0;
            Iterator it = stock.iterator();         
            while(it.hasNext())
            {
                Product item = (Product) it.next();
                System.out.println(""+item);
                found++;
            }
            if(found > 0)
            {
                return null;
            }
            else
            {
                return ((Product) it.next());
            }
        }
     
        /**
         * Locate a product with the given ID, and return how
         * many of this item are in stock. If the ID does not
         * match any product, return zero.
         * @param id The ID of the product.
         * @return The quantity of the given product in stock.
         */
        public int numberInStock(int id)
        {
            return 0;
        }
     
        /**
         * Print details of all the products.
         */
        public void printProductDetails()
        {
            Iterator it = stock.iterator();
            while(it.hasNext()) 
            {
            System.out.println(it.next());
            }
        }
    }
    Last edited by Universalsoldja; February 3rd, 2010 at 02:19 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem accessing specific data in an array and getting it to return properly

    Hello there,

    What you want to do then is try to loop through the products like you are now and then compare the argument id with the products id and if a match is found you return it.

        /**
         * Try to find a product in the stock with the given id.
         * @return The identified product, or null if there is none
         *         with a matching ID.
         */
        public Product findProduct(int id)
        {
            Iterator it = stock.iterator();         
     
            while(it.hasNext())
            {
                Product item = (Product) it.next();
     
                // Check the product id matches the id we are looking for and if it does, return the product
                if(product.getId() == id) {
                    // The reason we return the item here is that there is no point to keep
                    // looping because there can only be one product with that id.
                    return item;
                }
            }
     
            // If no match is found in the above loop the code will drop down to here and return null
            return null;
        }

    Simples!

    // Json

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

    Universalsoldja (February 4th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Problem accessing specific data in an array and getting it to return properly

    I think it is important you understand what you did wrong in your code, effectively you were looping through the whole list and not checking to see whether the Product had been matched, once the complete loop had been finished the found variable would be equal to the number of items in the list you then attempted to return the next item in the list, as the list had been iterated through completely there was no next item, this then returned null.

    Json code will work, just thought it was important you understood where you had gone wrong, so you can learn from it

  5. The Following 2 Users Say Thank You to JavaDaveUK For This Useful Post:

    Json (February 4th, 2010), Universalsoldja (February 4th, 2010)

  6. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem accessing specific data in an array and getting it to return properly

    Gentlemen and scholars, the both of you. Thank you for taking the time to explain the coding to me.

Similar Threads

  1. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM
  2. Need some general and specific advice.
    By Morevan in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2010, 11:31 PM
  3. problem in retrieving data via RMS
    By solaleh in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: August 30th, 2009, 05:46 AM
  4. Problem on sending vectors from Java server to C# client
    By MS_Dark in forum Java Networking
    Replies: 2
    Last Post: July 7th, 2009, 02:35 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM