Conn kal
Conn kal

Reputation: 11

How do you pass an ArrayList, created in another method, to the main method for further use there?

I'm trying to practice using ArrayLists and created a custom object class to do it. Trying to figure out a way to create the arraylist via a separate method then return it to the main method for later use in the program.

// Imports ArrayLists, Scanner class, Date objects, and try/catch exceptions
import java.util.*;
// Import the File class, FileWriter class, and IOExceptions
import java.io.*;

/**
*  The program will simulate a class registry system
*  Using multiple methods, the program will intake user input to edit the registry
*  After the user is done, the program will ask if the user would like to save the registry to a separate txt file.
*  If ordered to save, the program will then print the path to find the txt file.
*/

public class StudentsMainMeth {
   public static void main(String[] args) throws StudentException {
   
      System.out.println("These are the students currently enrolled:");
      createRoster();
      
      System.out.println("\n\t\t\t**Enrolled Students**");
      for(int i = 0; i < classRoster.size(); i++) {
      
         System.out.println(classRosteer.get(i));
         
      }
   }
   /**
    * Creates the ArrayList "classRoster" to store the custome objects from the Student class
    * The class will use three instance variables
    *    name
    *    grade
    *    gpa
    * the array will add five objects to start and return the ArrayList to the main method
    */
   public static ArrayList<Student> createRoster () throws StudentException {
   
      ArrayList<Student> classRoster = new ArrayList<>();
      classRoster.add(new Student(Aang, 12, 3.5));
      classRoster.add(new Student("Katara", 12, 4.0));
      classRoster.add(new Student("Soka", 12, 4.0));
      classRoster.add(new Student("Toph B.", 1, 4.0));
      classRoster.add(new Student("Zuko", 8));
      return(classRoster);
      
   }
}

When trying to compile, an error comes up that the array list isn't identified as a useable symbol.

StudentsMainMeth.java:25: error: cannot find symbol
      for(int i = 0; i < classRoster.size(); i++) {
                         ^
  symbol:   variable classRoster
  location: class StudentsMainMeth
StudentsMainMeth.java:27: error: cannot find symbol
         System.out.println(classRosteer.get(i));
                            ^
  symbol:   variable classRosteer
  location: class StudentsMainMeth
2 errors

Am I missing something that can identify it to the main method when the ArrayList is returned or do you need to create the ArrayList in the main method for it to be acknowledge for later use?

I know you can make the program to print the ArrayList straight after the method returns it, but the point is to be able to use it in the main method after being created in a separate method. I've done this before in a separate program but resorted to going from one method to another to pass the created array list but how do you pass it to the main method with it still being usable later ?

Upvotes: 0

Views: 67

Answers (1)

Roman C
Roman C

Reputation: 1

It prints the variable is not defined. Then you should create a class variable:

public class StudentsMainMeth {
  private  List<Student> classRoster = createRoster():
 // The main method and other stuff
 ...

Then it will fail on non-static variable accessing from the static main method. Then you should remove this variable from the main method. Better create another non-static method, and call it from the main using instance of StudentsMainMeth class.

private void mainMethodImpl() {
     
  System.out.println("These are the students currently enrolled:");
  //createRoster(); not needed here 
  System.out.println("\n\t\t\t**Enrolled Students**");
 
  for(int i = 0; i < classRoster.size(); i++) {
    System.out.println(classRosteer.get(i));         
  }
}

Then the main method will look like

public static void main(String[] args) throws StudentException {
 StudentsMainMeth instance = new StudentsMainMeth();
 instance.mainMethodImpl();
}
  

You should know that main method is only an entry point of your application. You should not place all the code inside it. Instead create classes and variables that you can call from it.

Upvotes: 0

Related Questions