0

I am trying to assign the values to the array products[] using the array arr[] which is passed in the constructor. When I compile the code I get the error

Shop.java:10: error: not a statement
products[]=arr.clone();
^ Shop.java:10: error: ';' expected
products[]=arr.clone();

How can I resolve the error and assign the arr[] values to the products[]

  import java.util.Scanner;
  public class Shop{
    private String shopName;
    private String shopAddress;
    private String products[];

    public Shop(String name,String add, String[] arr){
        shopName=name;
        shopAddress=add;
        products[]=arr.clone();
    }
}

I haven't given all the code, but assume

arr[]={"apple","orange","mango","banana"}

0

2 Answers 2

2

You should use products variable without [], because [] belongs to String to make it a String array, and your variable is products:

class Shop{
     private String shopName;
     private String shopAddress;
     private String products[]; // to avoid confusion I'd use String[] products;

 public Shop(String name,String add, String[] arr){
     shopName=name;
     shopAddress=add;
     products = arr.clone();
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the code your variable's name is "products". No need for [] after products.

try

  products = arr.clone();

Comments

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.