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 quantity
- is the name of the ArrayList
- is the name of the class that contains the info of ID, Name, and Quantity.
Actual Code
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. :frusty:
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.
Re: Need to find a certain information an object holds in an array.
Sorry about that.
Product Class
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.
Code:
public class StockManager
{
// A list of the products.
private ArrayList<Product> stock;
/**
* Initialise the stock manager.
*/
public StockManager()
{
stock = new ArrayList<Product>();
}
}
Re: Need to find a certain information an object holds in an array.
Quote:
There is an error in the code
Now could you post the full text of the error message?
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.
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.
Quote:
Originally Posted by
wondertwins
That was the entire message. I'm using BlueJ if that helps a bit.
Quote:
Originally Posted by
Fubarable
Again, please post the full error message.
Again, please indicate which line of code causes the error.
.................
1 Attachment(s)
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.
Code:
if(id == stock.contains(id)){
Attachment 3030
Re: Need to find a certain information an object holds in an array.
OK, now we're getting somewhere.
This makes no sense:
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.
2 Attachment(s)
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?
Code:
{
for(Product product : stock){
for(int i = 0 ; i < stock.size() ; i++){
if(stock.contains(id)){
int quantity = product.getQuantity();
}
else{
return 0;
}
}
}
}
Attachment 3031
Attachment 3032
Re: Need to find a certain information an object holds in an array.
Quote:
Originally Posted by
wondertwins
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.