简体   繁体   中英

Java error: Cannot find symbol (when calling an arrayList with add)

I am writing a program in Java which takes a chaotic list with random objects and sorts the student, teacher and professor objects into arraylists, and then prints those arrayLists.

Everything is working (so I will not post the other class files here) except whenever I try to call the arrayLists from somewhere it says "error: cannot find symbol". I couldn't find a solution for my problem elsewhere on stack overflow.

Here is the code:

import java.util.ArrayList;
public class Main {
  public static void main(String[] argv) {
    arrayList<Object> onlyTeachers = new ArrayList<Object>();
    arrayList<Object> onlyStudents = new ArrayList<Object>();
    arrayList<Object> onlyProfessors = new ArrayList<Object>();
// Do not change below
ArrayList<Object> chaos = new ArrayList<Object>();
chaos.add(new Teacher("deWitt, Booker", "Collar Avenue 45", 2222));
chaos.add(new Student("Johnsen, John", "the Road 6", 1231231));
chaos.add(new Student("Pun, Peter", "Applestreet 4", 1234));
chaos.add(new Boolean(false));
chaos.add(null);
chaos.add(new Teacher("Wiering, John", "Puppetlane 1", 7979786));
chaos.add(new Student("Cheese, Anna", "Cheesemarket 1", 455656));
chaos.add(new Teacher("White, Snow", "Fairy tale lane 3", 7889867));
chaos.add(new Student( "Peterson, Peter", "Canalstreet 3", 8998));
chaos.add(new Professor("dr.","Manson, Derrick", "Zakuroad 124", 899844));
chaos.add(new Student("Whiskers, Hettie", "Anotherroad 3", 9123));
chaos.add(new Double(10));
chaos.add(new Student("deGroot, Lambert", "Chirplane 2", 89444498));
chaos.add(new Professor("BSc.", "Pan, Peter", "Swinkelroad 2", 892438));
chaos.add(new Student("Bali, Ali", "Aroundthecorner 662", 8923498));
chaos.add(new Integer(12));
chaos.add(new Teacher("Benson, Ben", "Somewhere 25", 8963298));
chaos.add(new Student("Youssouf, Mohammed", "There 17", 89364698));
// Do not change the above

for(Object x : chaos){
  cleanUp(x);
}    
  System.out.println("\n\n\n");
for(Object x : onlyProfessors){
  System.out.println(x);
  System.out.println("\n");
}
  System.out.println("\n\n\n");
for(Object x : onlyStudents){
  System.out.println(x);
  System.out.println("\n");
}
  System.out.println("\n\n\n");
for(Object x : onlyTeachers){
  System.out.println(x);
  System.out.println("\n");
}

}
public static void cleanUp(Object object){  
  if(object instanceof Person){

    if(object instanceof Professor){  
       System.out.println("\nThis is a professor.");
       onlyProfessors.add(object);
      } else {
      if(object instanceof Student){
        System.out.println("\nThis is a student.");
        onlyStudents.add(object);
      } else {
        if(object instanceof Teacher){
          System.out.println("\nThis is a Teacher.");
          onlyTeachers.add(object);
        }
      }
    }
  } else {
    System.out.println("\nThis is not a person.");
  }

System.out.println(object);
}

}  

Sorry for the bad formatting where applicable.

There seems to be two problems in your code. First you need to use ArrayList instead of arrayList. Also it seems you are trying to access Arraylists in different methods when it is defined in a method locally. If a variable is defined within a method you cannot access it in other methods. So you may need to define your arraylists globally as your class variables and not inside your main function. Here is the code change required:

public class Main {

    static ArrayList<Object> onlyTeachers = new ArrayList<Object>();
    static ArrayList<Object> onlyStudents = new ArrayList<Object>();
    static ArrayList<Object> onlyProfessors = new ArrayList<Object>();

  public static void main(String[] argv) {

// remove the list from here
//    arrayList<Object> onlyTeachers = new ArrayList<Object>();
//    arrayList<Object> onlyStudents = new ArrayList<Object>();
//    arrayList<Object> onlyProfessors = new ArrayList<Object>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM