Skip to content
Advertisement

Use an arraylist multiple times inside different parts of JAVA code

I am using an array list to store values from user input by constructing an interactive menu for them to choose. My two choices so far, provides the user to input data to the list and to read the whole content of a list. The code I created so far consists of two classes.

My main class,

JavaScript

and my personnel class with getter and setter methods in order to store the data from user input,

JavaScript

My problem is that I want to use the catalog list in option 1 using a foreach loop but I can’t since I am getting a “Cannot resolve symbol catalog” error as showing in the code. What am I doing wrong?

Advertisement

Answer

I am not going to argue about the correctness of the solution, and the different way to implement it. However, if you want to solve that compilation error, it is just a matter of variable scope: You just need to move the creation of the catalog list at the beginning of the main function, in a way to increase its scope, for example you can put it as first statement, like this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement