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 8 of 8

Thread: Trouble with while loop and the use of the Iteration class.

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

    Default Trouble with while loop and the use of the Iteration class.

    I am having a hard time with using the Iteration class. I don't know how to effectively use the "next()" method, it confuses me. When I search for the last product in the list nothing prints out because at that time it is looking at the next item in the list. Do I use a variable that I subtract 1 from? Here is my code, any help would be awesome.

      public Product findProduct(int id)
        {
            Iterator<Product> p = stock.iterator();
            boolean looking = true;
            while(looking && p.hasNext())        {
                int searchID = p.next().getID();
                if (p.hasNext() && id == searchID)
                    {
                        Product foundProduct = p.next();
                        looking = false;
                        return foundProduct;
                    }
            }
            return null;
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    What is the relationship of the items in the list between the Product with the id == searchId
    and the following item in the list that is being returned?
    For example if the item at location x in the list has the matching id,
    then the code returns the item at location x+1


    What is the method: findProduct() supposed to find and return?
    If you don't understand my answer, don't ignore it, ask a question.

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

    simpson_121919 (February 17th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    The searchID holds the the ID field instance in the Product class. It retrieves it through getID method located in the Products class. The ID == searchId is to test and see if the parameter passed ID matches something in stock(via an arraylist, stock).

    The findProduct() is supposed to find the parameter ID variable and see if that ID is in stock. If yes it returns Product info, if not it returns null.

    Thanks for the quick response. If my iterator is always looking for the next what purpose does it have when looking for something at the end of a list?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    Here is what the code is doing. Say its searching for a record with id=2
    read id=1 no match
    read id=2 match
    read id=3 and return this one

    Is that what you want the code to do? Or do you want to return the record with id=2?

    The iterator tells you if there are any more records available to be returned by the next call to the next() method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    yes I was hoping to return the record with id=2. I redid the method just using a variable instead of an Iterator object.

    public Product findProduct(int id)
        {
            int index = 0;
            boolean looking = true;
            Product details = stock.get(index);
            while(looking && index <= stock.size())
            {
                int detailNum = details.getID();
                if (detailNum == id)
                {
                    looking = false;
                    return details;  
                }
                index++;
            }
            return null;
        }

    I was hoping to do away with the index variable because I don't like having a variable synced with an ArrayList index. I seems unreliable. I thought Iterator was my answer but it seems that it is not good for my situation. Thanks for the help.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    The iterator should work. The problem was the extra call to next() after the Product object with the desired id was found. return the p object with the matching id, don't get another one after the match was found.

    The problem with the code was it did not save the Product object that was returned by next(). It looked at its id and threw it away by getting the next object from the iterator.

    Where did you learn to code this way:
    int searchID = p.next().getID(); 
    //  the  object returned by next() is NOT saved
    Instead of this way:
    Product prd  = p.next();    // get the object and save a reference
    int searchID = prd.getID();
    //  here prd has a reference to the object with the id
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    Oh I see what you're saying. By invoking the next method again I am using next item instead of the intended one that didn't get referenced because I am chaining when I shouldn't be.

    I am learning with "Objects First with Java by David Barnes and Micael Kolling. In the book they brought up the idea of chaining with anonymous objects and mention that I should be familiar with it because I might see it in other code. I was trying to apply this concept but I see now that until I am more comfortable with programming I will not chain. I think at this stage I just need to keep it simple.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with while loop and the use of the Iteration class.

    I think at this stage I just need to keep it simple.
    That will be a good idea. Later when you understand a little more you could use chaining, but it really doesn't save much.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trouble with isolating a particular address in a for loop
    By DamageInc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2012, 04:52 PM
  2. Having trouble with for loop statement.
    By Truck35 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 25th, 2012, 04:45 PM
  3. [SOLVED] Trouble with my while loop
    By masterhand89 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 17th, 2012, 12:45 PM
  4. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  5. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM