I'm trying to check whether an array of class type objects contain a specific string.

This is the constructor I have for Product object:

public Product()    
{
    name = "No name yet";
    demandRate = 0;        
    setupCost = 0;
    unitCost = 0;        
    inventoryCost = 0;
    sellingPrice = 0;        
}      

This is the initialisation of the array:

 Product[] product = new Product[3];

I found similar questions here Checking if long is in array and here Look if an array has an specified object. So I tried this code:

public boolean isAProduct(String nameOfProduct)
    //Returns true if a name has been found otherwise returns false
    {
        boolean found = false;
        int counter = 0;

        while (!found && (counter < MAXNUMBEROFPRODUCTS))
        {                
            if (Arrays.asList(product).contains(nameOfProduct))
            {                   
                found = true;
            }
            else 
            {
                counter++;
            }
        }        

        return found;
    }

But this doesn't work as it allows me to enter the same name for a product twice. So my question is, is what I'm attempting even possible? If not, how could I go about solving the problem?

Any advice would be greatly appreciated.

share|improve this question
1  
That is not how it works in Java. You need to walk through each "Product" object and compare "nameOfProduct" with "name" of "Product" object. – Nambari Jun 6 '14 at 4:03
    
@Nambari Could you provide an example of how I would do that please? :) – MrPigeon Jun 6 '14 at 4:05
1  
I know you are doing class assignment, so I don't want to do code for you. But here is skeleton, Loop through Product[], get Product, then compare this Product--> "Name" with "nameOfProduct". – Nambari Jun 6 '14 at 4:07
    
For future reference, "array of class objects" means something like Class<?> [] = new Class<?>[] { Object.class, Integer.class, System.class };. You've just got an array of Objects. – David Ehrmann Jun 6 '14 at 5:39
up vote 1 down vote accepted

You need to create a get method to your name of product inside the Product class to enable you to get the data you want to check on each iteration of the array. you cant just compare an object with a string without accessing the String.

solution:

create a getter method in your Product class

public String getName()
{
   return this.name;
}

Iterate to all the Product class and compare string by calling the getter method for the name of the product

for(int i = 0; i < current_size_product; i++)
{
  if(product[i].getName().contains(string))
    //true
}
share|improve this answer
    
Thank you, I was able to solve the problem using your advice :) – MrPigeon Jun 6 '14 at 4:28
    
@MrPigeon you are welcome :)) – Rod_Algonquin Jun 6 '14 at 4:31
    
Note that getName() is technically unnecessary. It's not required for your program to run; you can just use product[i].name if name is public. "Getter methods" help make it easier to develop large programs with less bugs, but they are not absolute requirements. – immibis Jun 6 '14 at 4:46
    
@immibis it is OOP you need to encapsulate your data. – Rod_Algonquin Jun 6 '14 at 4:48
    
It is not OOP, it is basic Java programming. When you know how to program, then you can learn OOP design principles. – immibis Jun 6 '14 at 4:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.