The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Using a mutator method to set data to an array element

Hey guys, the problem I'm having is I want to use a mutator method to set data for an array element. The code I have so far is:

public void addProduct(String productName)
//Goes through and sets the name of a product and assigns it to the array
{
int index;
for (index = 0; index < product.length(); index++)
{
product[index].setName(productName);
numberOfProducts++;
}
}

The array was initialised like this:

Product[] product = new Product[3];

And the setName(String) method is just your typical mutator method.

However, in Eclipse, I have an error messages. It is:

"-The method setName(String[]) is undefined for the type String"

Does anyone have any advice on what the problem is and how I could solve it? Unfortunately, arrays are something that I still haven't quite got my head around.

Re: Using a mutator method to set data to an array element

I'm trying to use the mutator method setName(String) to set the values of each element of an array. So the first string entered by the user would be assigned to product[0] and so on. On an earlier assignment we weren't allowed to use arrays and so it was simple. All I had to do was something like:

if (numberOfProducts == 0)
{
product1.setName(product);
numberOfProducts++;
}//etc until all the product objects have had a name assigned

And that is the entire error message. After I wrote the line ( product[index].setName(productName); ), a little error icon appears next to the line(at least in Eclipse) and when I hover my mouse over the icon, that warning is all that it says. I'm not trying to be ambiguous, so I'm sorry if it comes off that way.