0
import java.util.ArrayList;
import java.util.Arrays;

public class Dragon {


    private String[] interests = new String[4];

    public Dragon(String[] interes) {

        setInterests(interes);  
    }

    public void setInterests(String[] interes) {
        interests = interes;
    }
    public String[] getInterests() {
        return interests;
    }

    @Override
    public String toString() {
        return "Dragon [interests=" + Arrays.toString(interests)"]";
    }   
}

Main class DragonMain

public class DragonMain {

    public static void main(String[] args) {

        Dragon test = new Dragon(args);

        test.setInterests(interes);

    }
}

I am not sure where I am meant to enter the data for the array as I keep getting errors.

Error:

interes cannot be resolved to a variable.

1 Answer 1

1

You haven't initialized array yet in main method.

public static void main(String[] args) {

    Dragon test = new Dragon(args);

    String[] interes = {"Xyz"};
    test.setInterests(interes);

}

Missing + in string concatenation.

@Override
    public String toString() {
        return "Dragon [interests=" + Arrays.toString(interests)+"]";
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I have another problem. I get a [Ljava.lang.String;@7ba4f24f error when I do test.getInterests() . However if I add another dragon object (and add it to my friends) and then initialise the interests and print my friends at index 0, the dragon shows all of the correct information with its interests.
test.toString() Dragon string represenation, and test.getInterests().toString will give hash value of array from object class.
Ok I understand. TY

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.