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.

Cale
  • 117
  • 1
  • 3
  • 18
  • 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. – kosa Jun 06 '14 at 04:03
  • @Nambari Could you provide an example of how I would do that please? :) – Cale Jun 06 '14 at 04: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". – kosa Jun 06 '14 at 04: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. – David Ehrmann Jun 06 '14 at 05:39

1 Answers1

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
}
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thank you, I was able to solve the problem using your advice :) – Cale Jun 06 '14 at 04:28
  • 1
    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. – user253751 Jun 06 '14 at 04:46
  • @immibis it is OOP you need to encapsulate your data. – Rod_Algonquin Jun 06 '14 at 04:48
  • It is not OOP, it is basic Java programming. When you know how to program, *then* you can learn OOP design principles. – user253751 Jun 06 '14 at 04:53
  • @immibis While it is basic Java programming, It is also a uni assignment. The professor wants us to implement basic ideas of encapsulation and information hiding. One of the requirements being that instance variables are declared private. – Cale Jun 06 '14 at 05:12
  • 1
    Then do use a getter of course. I had no way of knowing that from your question and I guessed wrong. – user253751 Jun 06 '14 at 05:13
  • @immibis Haha all good. I appreciate your input nonetheless :) – Cale Jun 06 '14 at 05:20