Java - Checking whether an array of objects contains a certain string

10,151

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:
10,151

Related videos on Youtube

Cale
Author by

Cale

Software Developer at Pegasus. I enjoy designing, creating and playing games in my free time.

Updated on June 04, 2022

Comments

  • Cale
    Cale almost 2 years

    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.

    • kosa
      kosa almost 10 years
      That is not how it works in Java. You need to walk through each "Product" object and compare "nameOfProduct" with "name" of "Product" object.
    • Cale
      Cale almost 10 years
      @Nambari Could you provide an example of how I would do that please? :)
    • kosa
      kosa almost 10 years
      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".
    • David Ehrmann
      David Ehrmann almost 10 years
      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.
  • Cale
    Cale almost 10 years
    Thank you, I was able to solve the problem using your advice :)
  • user253751
    user253751 almost 10 years
    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.
  • Rod_Algonquin
    Rod_Algonquin almost 10 years
    @immibis it is OOP you need to encapsulate your data.
  • user253751
    user253751 almost 10 years
    It is not OOP, it is basic Java programming. When you know how to program, then you can learn OOP design principles.
  • Cale
    Cale almost 10 years
    @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.
  • user253751
    user253751 almost 10 years
    Then do use a getter of course. I had no way of knowing that from your question and I guessed wrong.
  • Cale
    Cale almost 10 years
    @immibis Haha all good. I appreciate your input nonetheless :)