Results 1 to 12 of 12
- 02-15-2012, 01:28 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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:
- method basically tells you the product name, ID, and quantityJava Code:
toString
- is the name of the ArrayListJava Code:
stock
- is the name of the class that contains the info of ID, Name, and Quantity.Java Code:
Product
Actual Code
There is an error in the code and I have exhausted my options on how to get this code to work properly.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; }
-
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.
- 02-15-2012, 01:46 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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>(); } }
- 02-15-2012, 01:47 AM #4
Re: Need to find a certain information an object holds in an array.
Now could you post the full text of the error message?There is an error in the code
- 02-15-2012, 01:49 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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.
-
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.
- 02-15-2012, 05:09 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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.
-
Re: Need to find a certain information an object holds in an array.
- 02-15-2012, 05:21 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
-
Re: Need to find a certain information an object holds in an array.
OK, now we're getting somewhere.
This makes no sense:
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).Java Code:if (id == stock.contains(id)) { }
You need to figure out what exactly you want to test here and change your code to do that test.
- 02-15-2012, 06:14 AM #11
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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; } } } }
- 02-15-2012, 10:13 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need to find a certain information an object holds in an array.
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
-
Compile Array information
By viviosoft in forum AndroidReplies: 3Last Post: 09-09-2011, 11:57 PM -
How to add additional information to the exception object that can be retrieved later
By ouou in forum New To JavaReplies: 8Last Post: 05-20-2011, 09:50 AM -
How to find the JDBC Driver API Implementation Version information.
By talasilasumanth in forum JDBCReplies: 0Last Post: 05-09-2011, 09:37 AM -
Printing Object Information
By dom12 in forum New To JavaReplies: 10Last Post: 11-04-2010, 06:39 PM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks