编译错误:类型不匹配

时间:2014-03-15 13:27:36

标签: java arraylist compiler-errors

更改了我的代码,因为我应该保护我的代码免受学生侵害抄袭。

当我尝试编译代码时出现此错误:

Unresolved compilation problem:     
Type mismatch: cannot convert from Object to Entry      
at SchoolManager.listGrade(SchoolManager.java:49)   
at SchoolManager.main(SchoolManager.java:30)    

mainClass.java

    public class mainClass {

    static Scanner scanner = new Scanner(System.in);
    static ArrayList<C> c = new ArrayList();
    static ArrayList<Object> ob = new ArrayList();
    public static void main(String [] args){
        while(true){
            System.out.println("----------------------------------------------------------------------------");
            System.out.println("New:1 Add:2 ListC:3 ListOb:4 AddE:5 ListE:6");
            System.out.println("----------------------------------------------------------------------------");
            System.out.print(">> ");
            String option = scanner.nextLine();
            if(option.equals("0")){
                System.out.println(c.size()+" "+ob.size()+" ");
                System.out.println("Bye");
                break;
            }else if(option.equals("1")){
                c.add(createC());
            }else if(option.equals("2")){
                ob.add(createOb());
            }else if(option.equals("3")){
                listC();
            }else if(option.equals("4")){
                listOb();
            }else if(option.equals("5")){
                addE(); 
            }else if(option.equals("6")){
                listE(); 
            }else{
                System.out.println(" ");
            }

        }
    }

    private static void listE() {
        System.out.print("");
        String ob1 = scanner.nextLine();
        Object ob = findOb(ob1);
        if(ob==null){
            System.out.println(" ");
        }else{
            System.out.println(" ");
            System.out.println(ob);
            System.out.println("  ");
            for(int i=0;i<ob.getE().size();i++){ // this part is a problem.
                Entry e = ob.getE().get(i); // this part is a problem.
                System.out.println(e.getC()+"\t"+ge.getT()+"\t"+ge.getE());
            }
        }
    }

    private static void addE() {
        System.out.print(" ");
        String ob1 = scanner.nextLine();
        Object ob = findOb(ob1);
        if(ob==null){
            System.out.println("Could not be found...");
        }else{
            addE(ob);
        }
    }

    private static Object findOb(String ob1) {
        for(int i=0;i<ob.size();i++){
            if(ob.get(i).getID().equals(ob1)){
                return ob.get(i);
            }
        }
        return null;
    }

    private static void addE(Object ob) {
        System.out.print("? ");
        String c_id = scanner.nextLine();
        C c = findC(c_id);
        if(c==null){
            System.out.println("Could not be found...");
        }else{
            System.out.print("T? ");
            String t = scanner.nextLine();
            System.out.print("E? ");
            String ee = scanner.nextLine();
            ob.AddE(new Entry(c,t,ee));
        }
    }

    private static C findC(String c_id) {
        for(int i=0;i<c.size();i++){
            if(c.get(i).getID().equals(c_id)) return c.get(i);
        }
        return null;
    }

    private static void listOb() {
        for(int i=0;i<ob.size();i++){
            System.out.println(ob.get(i));
        }
        System.out.println();
    }

    private static void listC() {
        for(int i=0;i<c.size();i++){
            System.out.println(c.get(i));
        }
        System.out.println();
    }

    private static Object createObject() {
        System.out.print("S? ");
        String id = scanner.nextLine();
        System.out.print("N? ");
        String n = scanner.nextLine();
        System.out.print("S? ");
        String s = scanner.nextLine();
        return new Student(id,n,s);
    }

    private static C createC() {
        System.out.print("C? ");
        String id = scanner.nextLine();
        System.out.print("Ct? ");
        String Ct = scanner.nextLine();
        return new Course(id,Ct);
    }
} 

Object.java:

    public class Object {

    public String ID;
    public String n;
    public String s;
    public ArrayList<C> c;
    public ArrayList<Ob> s;
    public ArrayList<Entry> e;

    public Object() {

    }
    public Object(String id, String n, String s) {
        this.ID = id;
        this.n = n;
        this.s = s;
        c= new ArrayList<C>();
        ob= new ArrayList<Ob>();
        e= new ArrayList<Entry>();
    }

    public String getId() {
        return ID;
    }

    public String getN() {
        return n;
    }

    public String getS() {
        return s;
    }

    public String getID() {
        return getId();
    }

    public void AddE(Entry e) {
        e.add(Entry); //this part is a problem.
    }

    public ArrayList<Object> getE() {
        return ob; //this part is a problem.

    }

    public String toString() {
        String result = ID + "    " + n + "    " + s;
        for(int i=0; i < c.size(); i++) {
            result += c.get(i).toString();
            result += "\n";
        }
        return result;
    }
}

C.java:

    import java.util.ArrayList;


    public class C {

    String co;
    String t1;
    public ArrayList<Ob> ob;

    public C(String id, String t1) {
        co = id;
        this.t1 = t1;
        ob = new ArrayList<Object>();
    }

    public String getCo() {
        return co;
    }

    public String getT1() {
        return t1;
    }
    public String getID() {
        return getCo();
    }

    public String toString() {
        String result = co + "    " + t1;
        for(int i=0; i < ob.size(); i++) {
            result += ob.get(i).toString();
            result += "\n";
        }
        return result;
    }
}

Entry.java:

public class Entry {

    public C c;
    public String t;
    public String g;
    public ArrayList<Ob> g;


    public Entry(C c, String term, String gr) {
        c=c;
        t=t;
        gr=gr;
        g = new ArrayList<Ob>();
    }

    public Course getC() {
        return c;
    }

    public String getE() {
        return g;
    }

    public String getT() {
        return t;
    }

    public String toString() {
        String result = c+ "    " + t+ "    " + g;
        for(int i=0; i < gr.size(); i++) {
            result += gr.get(i).toString();
            result += "\n";
        }
        return result;
    }
}

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

这是它所说的错误:

GradeEntry ge = student.getGrades().get(i);

表示无法从Student转换为GradeEntry,因此我怀疑getGrades实际上正在返回Students,是的情况就是这样:

public ArrayList<Student> getGrades() {
    return students; //this part is a problem.
}

我认为你打算回归成绩:

public ArrayList<Grade> getGrades() {
    return gr;
}