1

So, first off, in my class Film I need to declare various fields including a string array of actors a fixed length (5). My default constructor needs to set these values for now but later I think I'll need the 5 string values to null so I can add actors up to the limit. I then want to be able to create instances of the class using values passed to a constructor and finally add actors to the string array of a given film - where there's a free spot (and throw an exception when there's not one).

What I have so far:

public class Film {
    private String title;
    private String[] actors = new String[5]; // can I set the limit here and use this string below?
    private double budget;

    // default constructor:
    public Film() {
        title = "Default title";
        // This creates a new string though and doesn't limit to 5 :o(
        authors = new String[] {"a","b","c","d","e"};
        budget = 1.1
    }

    // constructor which takes values passed in:
    public Film(String title, double budget, String[] actors) {
        this.title = title;
        this.budget = budget;
        this.actors = actors;
    }
}

In my main programme, I have the following which shows an error on the comma after the budget value and I can't work out why:

public class Main {

    Film homeAlone = new Film("Home Alone", 10.9, ("McCauley", "John", "Paul", "George", "Ringo"));

}

And as for the 'adding an actor' method - I don't know where to start. Any help would be greatly appreciated!

Cheers,

Mike

3
  • 1
    The syntax for passing an array is new String[]{"McCauley", "John", "Paul", "George", "Ringo"} Commented Apr 25, 2018 at 10:01
  • ...and also you don't have the variable authors. Commented Apr 25, 2018 at 10:03
  • budget = 1.1 missing semicolon after definition Commented Apr 25, 2018 at 10:26

4 Answers 4

2

You are initializing the array in this line

private String[] actors = new String[5];

and again in this line

authors = new String[] {"a","b","c","d","e"};

The first statement created an empty array of length 5 and it does restrict it to 5 elements. If you write below statements, it will throw an exception because array created has 5 bins only 0...4 and can hold only 5 elements

private String[] actors = new String[5];
actors[5]="John";

But again when you wrote the second statement you have reinitialized the previous array to 6 elements by passing 6 elements in the constructor itself and it created a new array of length 6 having those 6 elements you passed in the constructor.

Now again if you want to execute statement below it will throw an exception.

authors = new String[] {"a","b","c","d","e"};
authors[6]="f";

More about arrays in java here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I knew I was creating the array twice & I obviously don't want to do that, I was trying to work out where in the code to do the limiting to 5 and showed both my options - should've used comments rather than repeat the code. So if I declare an empty array of 5 strings using: private String[] actors = new String[5]; How do I then populate it with up to 5 values using a constructor? Do I have to do something like: actors[0] = "John"; actors[1] = "Paul"; Can I not populate it in one line using something like: actors = {"John", "Paul", "George", "Ringo", "Dave"};
best solution for your problem is given by @Bohemian, in the comments, it is also explained how to restrict that by adding a condition to check length of the argument and thrown an exception if it does not match your condition
2

Use a varargs for the actors:

public Film(String title, double budget, String... actors) {
    this.title = title;
    this.budget = budget;
    this.actors = actors;
}

Then you can create a new Film with any number of actors:

Film a = new Film("Foo", 1.1, "Ringo");
Film b = new Film("Bar", 1.1, "Ringo", "John", "Paul");

Delete the default constructor - it serves no useful purpose.
Don’t declare the array for the field; just code String[] actors;

2 Comments

I understand about the default constructor but I'm doing this as part of an exercise and that's part of my task. If I use varargs as you suggested, how do I limit the length of the array to 5?
@Bongo there are two main ways to limit it to five: 1) check the length of the actors parameter in the constructor and throw an exception if it's longer than five, 2) don't use varargs and instead create five constructors that take 1,2,3,4 and 5 String parameters for actors
1

you should instantiate your Film class as following:

Film film = new Film("Movie", 10.00, new String[]{"a", "b", "c","d","e"});

This because you want to pass an array to the Film constructor.

As for adding actors to a movie, you could add a function to the film class which verifies that the that there is a free spot, and if this is the case add it on the free spot.

2 Comments

But won't that create a new array that isn't limited to 5 values?
No it will create an array with a length based on the declaration
1

you must add semicolon ; after budget = 1.1

you have no variable called authors , perhapse you mean actors?

you cannot initialize a string array like

("McCauley", "John", "Paul", "George", "Ringo")

it must be new String[]{"McCauley", "John", "Paul", "George", "Ringo"}

i suggest you use an IDE like eclipse or netbeans, as it can give you a clue to all of these issues and how to fix them

EDIT based on OP question about limiting to 5: if you want to limit to 5 you can either :

  1. pass each name as a separate argument and populate the array in the constructor
  2. accept any size array and only populate the first 5

  3. validate by throwing an exception if array of size more than 5 is passed

4 Comments

Yup, apologies for the missing ; and the actors/authors, that was an oversight on my behalf when dumping the code into here but not one that is reflected in my actual code. Anyhoo, using new String[]{"McCauley"} creates a new array which won't be limited to 5 values. And I'm using eclipse.
@Bongo except you won't be using new String[]{"McCauley"} you should be using new String[]{"McCauley", "John", "Paul", "George", "Ringo"}, or do you mean you need to pass the constructor any given number of names and not 5 excatly? please check my edit
Thanks @user1 Option 3 sounds the better one to fit with how I need it to work. In which case I assume I can just declare the array by using private String[] actors; and then populate the array using constructors. Can you please give me a steer on throwing an exception if >5 values are passed in?
@Bongo if (array.length>5) throw new Exception("message");

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.