title: "can only iterate over an array"
body: <p><strong>Link to Marks.txt file I was using for Student records for I/O:</strong> <a href="http://www.mediafire.com/file/kksq6qvb1zllfpc/marks_test_no_empty_strings.txt" rel="nofollow noreferrer">enter link description here</a></p>
  
  <p>I have a <strong>Student</strong> class(contains 1 int studentID, and 6 assignment float scores) that is supposed to be filling another class called <strong>CourseSection</strong> (is just an ArrayList of Student objects), which is an ArrayList, with Student Objects.</p>
  
  <p>So each Student Object should fill up the ArrayList called <strong>CourseSection</strong> after the data is read from a Marks.txt file.</p>
  
  <p>I'm trying to write the search() method so it prompts the user for input of an StudentID, and then this int ID is used to traverse the ArrayList of Student objects until it .getID() == ID (the one the user input). THEN, it needs to display the corresponding students 6 assignment scores(floats). I'm getting a </p>
  
  <pre><code>Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  Can only iterate over an array or an instance of java.lang.Iterable
  </code></pre>
  
  <p>The error is coming from right after the for loop where is says "course2". Is java not reading this as an ArrayList? It's supposed to be an ArrayList of Student objects correct? because you can see I create the CourseSection object called "course2" first, then I read from the file Marks.txt and assign "course2" to equal this CourseSection.loadFrom method (which the method is returning a CourseSection object).</p>
  
  <pre><code>public static void search(){
  
          BufferedReader aFile;
  
          CourseSection course2 = new CourseSection(); //creates course object which is ArrayList of Student objects
  
          aFile = new BufferedReader( new FileReader("marks_test_no_empty_strings.txt"));
           course2 = CourseSection.loadFrom(aFile); // reads .txt into course ArrayList
  
          Scanner scanner =  new Scanner(System.in);
          int id;
          boolean foundStudent = true;
  
  
          System.out.print("Enter the Student's ID: ");
          id = enterID();
  
         for(Student s : course2) {
              if(s.getID() == id) {
                  s.getA1();
                  s.getA2();
                  s.getA3();
                  s.getA4();
                  s.getMidterm();
                  s.getFinalExam();
              }
              else
                  System.out.println("ID not found!!!");
          }
   }
  </code></pre>
  
  <p>and the <strong>CourseSection</strong> class if it helps:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Iterator;
  import java.io.*;
  
  public class CourseSection {
      private String name;
      private ArrayList&lt;Student&gt; students;
  
     /**
     * Initializes the ArrayList&lt;Student&gt;.
     */ 
    public CourseSection(String n)
    {
      name = n;
      students = new ArrayList&lt;Student&gt;();
    }
  
    public CourseSection()
    {
      students = new ArrayList&lt;Student&gt;();
    }
  
    /**
     * Will add a new student to the ArrayList.
     * @param s A Student object.
     */
    public void addStudent(Student s){
      students.add(s);
    }
  
    /**
     * Removes the selected student from the ArrayList.
     * @param s A Student object.
     */
    public void removeStudent(Student s){ 
      Iterator studentIterator = students.iterator();
  
      while(studentIterator.hasNext()){
        if(studentIterator.next() == s)
          studentIterator.remove();
      }
    }
  
  
    /**
     * Lists all the information for each student in the course section. 
     */
    public void listStudents(){
  
      for(Student s: students){
        System.out.println(s);  
      }  
    }
  
    /**
     * Reads aFile except for the header and continuously creates new student objects 
     * for a new CourseSection.
     * @param aFile File to read from.
     * @return CourseSection New CourseSection read from aFile
     */
    public static CourseSection loadFrom(BufferedReader aFile) throws IOException{
        //String line = aFile.readLine();
      CourseSection course = new CourseSection(aFile.readLine()); 
      aFile.readLine(); // skips line
      while (aFile.ready()) //read until no more available (i.e., not ready)
      { 
  
          course.addStudent(Student.loadFromST(aFile)); //read &amp; add the student
          }
  
      return course;
    }
  
  }
  </code></pre>
  
  <p>This is the file I use to test the <strong>loadFrom</strong> methods in the <strong>Student</strong> class, and to test <strong>loadFrom</strong> in the <strong>CourseSection</strong> class</p>
  
  <pre><code>import java.io.IOException;
  import java.util.InputMismatchException;
  import java.util.Iterator;
  import java.util.Scanner;
  
  public class loadTester {
  
      private static void studentLoadTest() throws IOException {
          BufferedReader  file1;
          Student student1; // student object
          //String line;
  
          file1 = new BufferedReader(new FileReader("marks_test_no_empty_strings.txt"));
  
          file1.readLine(); // skips 1st line
          file1.readLine(); // skips 2nd line
  
          /*
          while(file1.ready())
          {
              System.out.println(file1.readLine());
          }
          */
  
         /* while((line = aFile.readLine()) != null) {
              System.out.println(line);
          }
          */
         student1 = Student.loadFromST(file1); // using String Split
         System.out.println(student1); // This method individually parses it
  
         //file1.close();
      }
  
       private static void courseLoadTest() throws IOException {
  
              //String line;
           //CourseSection course = new CourseSection();
  
              BufferedReader aFile = new BufferedReader(new FileReader("marks_test_no_empty_strings.txt"));
  
              CourseSection course = CourseSection.loadFrom(aFile);
             // course.loadFrom(aFile);
              course.listStudents(); // this outputs the STUDENT OBJECTS IN THE ARRAY LIST!
  
              //course = CourseSection.loadFrom(aFile);
              //aFile.close();
          }
  
       public static void search(){
  
              BufferedReader aFile;
  
              CourseSection course2 = new CourseSection(); //creates course object which is ArrayList of Student objects
  
              aFile = new BufferedReader( new FileReader("marks_test_no_empty_strings.txt"));
               course2 = CourseSection.loadFrom(aFile); // reads .txt into course ArrayList
  
              Scanner scanner =  new Scanner(System.in);
              int id;
              boolean foundStudent = true;
  
  
              System.out.print("Enter the Student's ID: ");
              id = enterID();
  
             for(Student s : course2) {
                  if(s.getID() == id) {
                      s.getA1();
                      s.getA2();
                      s.getA3();
                      s.getA4();
                      s.getMidterm();
                      s.getFinalExam();
                  }
                  else
                      System.out.println("ID not found!!!");
              }
       }
  
              public static int enterID(){
  
                  Scanner scanner = new Scanner(System.in);
                  int id = 0;
  
                  try{
                    System.out.print("Enter the student's ID: ");
                    id = scanner.nextInt();
                  }catch(InputMismatchException e){
                    System.out.println("Error: InputMismatchException");
                    id = enterID();
                  }
  
                  return id;
                }
  
  
      public static void main(String[] args) throws IOException
      {
          System.out.println("Testing Student Object I/O Stream:");
          studentLoadTest(); // testing to see if it outputs 1 Student Object per line
          System.out.println();
          System.out.println("Testing CourseSection creating ArrayList of Student Objects I/O Stream:");
          courseLoadTest(); // this should output the Entire marks.txt file as an ArrayList of Student objects
          search();
      }
  }
  </code></pre>