Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    The method setName(String[]) is undefined for the type String
    Read the API doc to see what methods are in the String class. You can't make them up.

    What are you trying to do? Copy the full text of the error message and post it. The message should include the source line with the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    I don't understand why that statement gives that error.
    The variable: product is an array of Product not an array of String.

    I can't see the screen of your IDE to see what it is saying. The javac compiler gives good error messages that can be copy and pasted here.

    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    method setName(String[])
    The error shows a String array being passed in the setName() method. Where is the code that passes an array to setName()?

    Are you confusing what program you are working on? This statement:
                product1.setName(product);
    would give the error message you have posted if
    product1 is a String
    and product is an array of String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Mutator method at Beginner level
    By Huntea in forum Java Theory & Questions
    Replies: 3
    Last Post: January 30th, 2014, 07:47 AM
  2. Passing an element from an array to a Method
    By camaleonx13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2013, 06:36 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. Help with beginner mutator method
    By Dysun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 06:32 PM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM

Tags for this Thread