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,

package com.andrekreou;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to the personnel address book");
        System.out.println("In the following menu, a whole selection of services is provided");
        Scanner user_input = new Scanner(System.in);
        while (true){
            showMenu();
            String selection = user_input.next();

            if (selection.equals("1")){
                System.out.println("Below you can see all of the data being provided");

                for (String personnel : catalog) { }  //ERROR: Cannot resolve symbol catalog

            }else if (selection.equals("2")){
                ArrayList<String> catalog = new ArrayList<>();
                Personnel p1 = new Personnel();
                System.out.println("Please insert the data for the new contact");

                System.out.println("Input the fullname:");
                Scanner scan = new Scanner(System.in);
                String full_name = scan.nextLine();
                p1.setFull_name(full_name);
                catalog.add(full_name);
                System.out.println("You inserted the following fullname: "+p1.getFull_name());

                System.out.println("Input the phonenumber:");
                Scanner phone_number_input = new Scanner(System.in);
                String phone_number = phone_number_input.next();
                p1.setPhone_number(phone_number);
                catalog.add(phone_number);
                System.out.println("You inserted the following phonenumber: "+p1.getPhone_number());

                System.out.println("Input the address:");
                Scanner address_input = new Scanner(System.in);
                String address = address_input.nextLine();
                p1.setAddress(address);
                catalog.add(address);
                System.out.println("You inserted the following address: "+p1.getAddress());

                System.out.println("Εισάγετε την διεύθυνση e-mail:");
                Scanner email_input = new Scanner(System.in);
                String email = email_input.next();
                p1.setEmail(email);
                catalog.add(email);
                System.out.println("You inserted the following e-mail: "+p1.getEmail());

                System.out.println("Εισάγετε την ημερομηνία γέννησης:");
                Scanner date_of_birth_input = new Scanner(System.in);
                String date_of_birth = date_of_birth_input.nextLine();
                p1.setDate_of_birth(date_of_birth);
                catalog.add(date_of_birth);
                System.out.println("You inserted the following: "+p1.getDate_of_birth());

                System.out.println("Εισάγετε τον αριθμό ΑΜΚΑ:");
                Scanner AMKA_input = new Scanner(System.in);
                String AMKA = AMKA_input.next();
                p1.setAMKA(AMKA);
                catalog.add(AMKA);
                System.out.println("You inserted the following ΑΜΚΑ: "+p1.getAMKA());

            }
        }
    }
    static void showMenu(){
        System.out.println("1. View the whole contacts");
        System.out.println("2. Insert a new contact");
        System.out.println("Please give your choice");
    }
}

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

package com.andrekreou;

import java.io.Serializable;

public class Personnel implements Serializable {
    private String full_name;
    private String phone_number;
    private String address;
    private String email;
    private String date_of_birth;
    private String AMKA;

    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDate_of_birth() {
        return date_of_birth;
    }

    public void setDate_of_birth(String date_of_birth) {
        this.date_of_birth = date_of_birth;
    }

    public String getAMKA() {
        return AMKA;
    }

    public void setAMKA(String AMKA) {
        this.AMKA = AMKA;
    }
}

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:

public class Main {
   
   public static void main(String[] args) {
      ArrayList<String> catalog = new ArrayList<>();  // Creation of catalog list
      ...
   }
...
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement