Results 1 to 12 of 12
  1. #1
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Need to find a certain information an object holds in an array.

    Hey guys, I have an assignment that basically tells us to 'get' information of an object that is in an array based on the id number that I or the user inputs. The object is referenced to another class that contains the ID number, total number of quantity a product has, and the name of the product. However I am having a really hard time writing a code that tells how the arraylist should get the data. This is the code that is supposed to find the product of the id by the ID number and tell you the name of the product.

    Note:

    • Java Code:
      toString
      method basically tells you the product name, ID, and quantity


    • Java Code:
      stock
      is the name of the ArrayList


    • Java Code:
      Product
      is the name of the class that contains the info of ID, Name, and Quantity.





    Actual Code

    Java Code:
    public Product findProduct(int id)
        {
            for( Product product : stock){
             for(int i = 0 ; i < stock.size() ; i++){
               if(id == product.getID()){
                   product.toString();
                }
             
               }
          
           }
        return null;
        
       }
    There is an error in the code and I have exhausted my options on how to get this code to work properly.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need to find a certain information an object holds in an array.

    You may need to show more code and especially show the actual error message and indicate which line causes it. Our ability to read minds is probably less than you might think.

  3. #3
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need to find a certain information an object holds in an array.

    Sorry about that.

    Product Class

    Java Code:
    public class Product
    {
        // An identifying number for this product.
        private int id;
        // The name of this product.
        private String name;
        // The quantity of this product in stock.
        private int quantity;
    
        /**
         * Constructor for objects of class Product.
         * The initial stock quantity is zero.
         * @param id The product's identifying number.
         * @param name The product's name.
         */
        public Product(int id, String name)
        {
            this.id = id;
            this.name = name;
            quantity = 0;
        }
    }

    StockManager Class, which holds the arrays of products.
    Java Code:
    public class StockManager
    {
        // A list of the products.
        private ArrayList<Product> stock;
        
        
    
        /**
         * Initialise the stock manager.
         */
        public StockManager()
        {
            stock = new ArrayList<Product>();
        }
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default Re: Need to find a certain information an object holds in an array.

    There is an error in the code
    Now could you post the full text of the error message?

  5. #5
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need to find a certain information an object holds in an array.

    incomparable types: Boolean and Int.

    I have a blueprint of what I am going to do but I don't know how I would execute the plan.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need to find a certain information an object holds in an array.

    Again, please post the full error message.
    Again, please indicate which line of code causes the error.
    Last edited by Fubarable; 02-15-2012 at 02:04 AM.

  7. #7
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need to find a certain information an object holds in an array.

    That was the entire message. I'm using BlueJ if that helps a bit.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need to find a certain information an object holds in an array.

    Quote Originally Posted by wondertwins View Post
    That was the entire message. I'm using BlueJ if that helps a bit.
    Quote Originally Posted by Fubarable View Post
    Again, please post the full error message.
    Again, please indicate which line of code causes the error.
    .................

  9. #9
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need to find a certain information an object holds in an array.

    Gah I feel stupid. So sorry about that. It's the red highlighted area.

    Java Code:
    if(id == stock.contains(id)){
    Need to find a certain information an object holds in an array.-0ayn2.png

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need to find a certain information an object holds in an array.

    OK, now we're getting somewhere.

    This makes no sense:

    Java Code:
    if (id == stock.contains(id)) {
    
    }
    id is an int and stock.contains(...) returns a boolean -- true or false. So it's like you're asking if 3 == true which in Java again doesn't make sense (all you C/C++ folks keep out of this).

    You need to figure out what exactly you want to test here and change your code to do that test.

  11. #11
    wondertwins is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Need to find a certain information an object holds in an array.

    I changed the code up a bit but it is constantly giving me 0 as the return when I should be able to get a certain other value. For example, I changed the quantity of a product to 12 but when I call this method, it's skipping the for loop and heading straight to return 0. However if I try to put 'return 0' in the if statement, another error comes up saying that I need a return value. I also added another for loop that iterates but I'm not sure if it is necessary. How do I make it so that I can have a return value outside of the main for-loop?

    Java Code:
    {
            for(Product product : stock){
                for(int i = 0 ; i < stock.size() ; i++){
                if(stock.contains(id)){
                    int quantity = product.getQuantity();
                }
                else{
                    return 0;
                }
            }
        }
       }
    Need to find a certain information an object holds in an array.-pofml.png

    Need to find a certain information an object holds in an array.-jpkql.png

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need to find a certain information an object holds in an array.

    Quote Originally Posted by wondertwins View Post
    Java Code:
    {
            for(Product product : stock){
                for(int i = 0 ; i < stock.size() ; i++){
                if(stock.contains(id)){
                    int quantity = product.getQuantity();
                }
                else{
                    return 0;
                }
            }
        }
       }
    First, you are looping over your stock array twice for no reason.
    Just stick with the outer loop and get rid of the "for (int i = 0 etc etct)" one.

    Next, the 'id' you are looking for is in the Product, not the stock array.
    So compare the id against that of the product you are checking.

    Finally figure out how you need to return.
    The default (ie "no product found") quantity should be at the end of the method.
    The "found" quantity should be inside the loop if the ids match.

Similar Threads

  1. Compile Array information
    By viviosoft in forum Android
    Replies: 3
    Last Post: 09-09-2011, 11:57 PM
  2. Replies: 8
    Last Post: 05-20-2011, 09:50 AM
  3. Replies: 0
    Last Post: 05-09-2011, 09:37 AM
  4. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  5. Replies: 3
    Last Post: 04-07-2008, 06:02 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
  •