2

I'm trying to check whether an array of 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.

4
  • 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. Commented Jun 6, 2014 at 4:03
  • @Nambari Could you provide an example of how I would do that please? :) Commented Jun 6, 2014 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". Commented Jun 6, 2014 at 4:07
  • 1
    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. Commented Jun 6, 2014 at 5:39

1 Answer 1

5

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, I was able to solve the problem using your advice :)
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 it is OOP you need to encapsulate your data.
It is not OOP, it is basic Java programming. When you know how to program, then you can learn OOP design principles.
Then do use a getter of course. I had no way of knowing that from your question and I guessed wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.