You will have to declare newActor outside the scope of the switch statement. Inside case 2 of your switch, newActor doesn't exist.
int c = 0;
switch (c) {
case 1:
Object obj = new Object();
break;
case 2:
System.out.println(obj.toString()); // obj only exists in case 1
break;
}
This is actually a little peculiar and maybe somebody else can explain why, but the error you are getting seems to be particular to the switch statement. If you try to do the following:
int c = 0;
if (c == 1) {
Object obj = new Object();
} else if (c == 2) {
System.out.println(obj.toString());
}
You will get a different error that says "cannot find symbol" which is more to the point. Apparently cases inside a switch share scope such that they can "see each other". I assume this is related to the sans-break fall-through. Switches are all one block and the cases only determine the entrance point.
I'm not sure what you are trying to do to tell you where to put your newActor declaration but the point is right now case 2 in your switch can't use it. It only exists inside case 1.
Also you shouldn't use a switch for such a long statement. You should at least refactor it so it uses if statements. Switches are just not meant for this kind of thing. They are meant for long conditions with lots of possibilities and small statements for each case:
char operator = '*';
int leftNumber = 5;
int rightNumber = 9;
switch (operator) {
case '+': System.out.println(leftNumber + rightNumber);
break;
case '-': System.out.println(leftNumber - rightNumber);
break;
case '*': System.out.println(leftNumber * rightNumber);
break;
case '/': System.out.println(leftNumber / rightNumber);
break;
case '^': System.out.println(Math.pow(leftNumber, rightNumber));
break;
default: System.out.println("Unknown operator");
}
As a long side note, you are not passing a type parameter to your ArrayLists. Java doesn't know that your ArrayLists contain Actors and Films unless you say so:
ArrayList<Actors> actors = new ArrayList<Actors>();
ArrayLists and other generics are supposed to be initialized with a bracketed parameter of <E>
where E is the desired class type of the elements in the array.
The type parameter is a little bit like extending a class. The ArrayList class will have E all over the source code and when you pass Actor in to the E parameter, E is replaced with Actor.
class ArrayList<E> {
Object[] array = new Object[some_length];
public E get(int index) {
return (E)array[index];
}
}
becomes
class ArrayList<Actor> {
Object[] array = new Object[some_length];
public Actor get(int index) {
return (Actor)array[index];
}
}
Note that even then whether or not Java knows that that ArrayList contains Actors while the application is running is still vague due to the way generics are implemented. They are implemented using something called erasure. At runtime there is no way to know that the ArrayList is of the Actor type.
If you declare them with the type parameter, you will be able to access their elements using get()
as Actors and Films in your code which you won't be able to right now.
In other words you can do this:
System.out.println(actors.get(aIndex).filmList.get(fIndex));
Which it looks like you are starting to try to do under case 2 but you also won't be able to do at the moment. Without <Actor>
or <Film>
it will tell you "cannot find symbol" or "incompatible types" because the ArrayList holds an array of Object without the type parameter. (It's still an array of Object with the type parameter but you can use it like the type you pass in.)