0

My program reads a text file with student information and puts each line of data into a new Student object with various properties.

A loop then puts each of those Student objects into an ArrayList, allowing the student data to be searched using other methods.

However, the only way I have been able to make this work is to call the readStudent method in each search method, meaning the input file is processed and the ArrayList recreated repeatedly.

This is the method which reads the input file, creates the objects and puts the objects in the ArrayList:

public class readStudent
{

   public static List< Student > readStudent() throws Exception {
   Scanner input = new Scanner(new File("students.txt")); 
   List< Student > studentList = new ArrayList< Student >();

   while (input.hasNext()) {

        int id = input.nextInt();
        String lastName = input.next();
        String firstName = input.next();            
        int gradYear = input.nextInt();

        Student student = new Student(id, firstName, lastName, gradYear);
        studentList.add(student);

    }
    return studentList;
}

This is one of the search methods, also in the readStudent class (slightly edited for brevity):

 public static void allRecords() throws Exception {
    List< Student > studentList = readStudent.readStudent(); 
    int size = studentList.size();

    for (int i=0; i<size; i++) {

        System.out.printf("%-5d %10s %10s %15d\n", studentList.get(i).returnId(), studentList.get(i).returnFirst(), studentList.get(i).returnLast(), studentList.get(i).returnGrad());
    }

}

The other search methods all begin with List< Student > studentList = readStudent.readStudent(); to make the array available to be searched.

My question is this: Is it possible to perform the readStudent method once to create the ArrayList, perhaps in the beginning of main, then reference that ArrayList from the search methods without having to re-run the entire readStudent method every time?

1
  • Thanks very much to all who answered - I accepted the answer with the full example because as a novice programmer, I need all the help I can get! All of the answers were helpful and seem to point to the same solution, so as far as I'm concerned, they're all just as relevant. Commented Apr 23, 2014 at 16:05

5 Answers 5

1

A simple solution would be to create a class level List and initialize this only once at the begining of main. Then use this all across the code.

    public class readStudent
{

    private static List< Student > studentList;

    public static void main(String[] args)
    {
        studentList = readStudent();

    // all other code...
    }


    static someMethod(){

        // use studentList directly
    }


}

Just make sure your code is thread safe.

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

Comments

0

Yes, it is possible to only create the ArrayList once and perform operations on it on multiple other methods. A way to do this is as follows:

  1. Define a global variable at the top of class readStudent, say studentList
  2. Instead of returning a list in method readStudent(), add each new Student object to the global variablestudentList
  3. In each method call where you query studentList, simply refer to the global variable instead of running readStudent() again.

A sample implementation of this global variable may look like:

public class readStudent
{
       public static List<Student> studentList = new ArrayList<Student>();

       public static void main(String[] args) {
           readStudent(); //to initialize studentList
           //other methods here
       }

       public static void readStudent() throws Exception {
       Scanner input = new Scanner(new File("students.txt")); 

       while (input.hasNext()) {

            int id = input.nextInt();
            String lastName = input.next();
            String firstName = input.next();            
            int gradYear = input.nextInt();

            Student student = new Student(id, firstName, lastName, gradYear);
            studentList.add(student);
        }

    }
}

and for allRecords(),

 public static void allRecords() throws Exception 
 {
    int size = studentList.size();
    for (int i=0; i<size; i++) {
        System.out.printf("%-5d %10s %10s %15d\n", studentList.get(i).returnId(), studentList.get(i).returnFirst(), studentList.get(i).returnLast(), studentList.get(i).returnGrad());
    }
}

Comments

0

Yes you can make a global variable and reference your readStudent() to the List <Student> global variable so you dont need to rerun your readStudent method each time.

Comments

0

Declare the List< Student > studentList = new ArrayList< Student >(); outside the readStudent() Method, then if you call readStudent() method once it will get initialize and you can use it everywhere in ReadStudent class.

Comments

0

declare the list outside of any method in the start just after the class syntax like this

  class readstudent{
  public static List<student> xyz=new List();
  public static void main(String args[])
  {

   }
   public static somemethod()
   {

    }

  }

Comments

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.