Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol in same package and directory

I have two classes, Offering and Course. They are both in the same package and the same directory.

Offering.java:

package assignment02;

public class Offering implements Comparable<Offering> {
    private Course course;
    private int CRN;
    private int semester;

    public Offering(Course course, int CRN, int semester) {
        this.course = course;
        this.CRN = CRN;
        this.semester = semester;
    }

    public int getNumCredits() {
        return course.getNumCredits;
    }

    public int getCRN() {
        return CRN;
    }

    public int getSemester() {
        return semester;
    }

    public int compareTo(Offering other) {
        if(other == null) return - 1;
        return semester - other.semester;
    }
}

Course.java:

package assignment02;

public class Course {
    private String name;
    private String rubric;
    private String number;
    private int numCredits;

    public Course(String name, String rubric, String number, int numCredits) {
        this.name = name;
        this.rubric = rubric;
        this.number = number;
        this.numCredits = numCredits;
    }

    public String getName() {
        return name;
    }

    public String getRubric() {
        return rubric;
    }

    public String getNumber() {
        return number;
    }

    public int getNumCredits() {
        return numCredits;
    }
}

When I try to compile Offering, I get the errors:

D:\CS 140\assignment02>javac Offering.java
Offering.java:4: error: cannot find symbol
    private Course course;
            ^
   symbol:   class Course
   location: class Offering

and

Offering.java:8: error: cannot find symbol
    public Offering(Course course, int CRN, int semester) {
                    ^
   symbol:   class Course
   location: class OfferingOffering.java:8: error: cannot find symbol

I know that the error means the compiler is unable to do anything with 'Course,' but I don't really know why. I also know that it will end up being something incredibly obvious, but I just can't seem to figure it out. Any help would be really appreciated.

like image 715
John Avatar asked Sep 16 '15 01:09

John


People also ask

How do I get rid of Cannot find symbol error?

Output. In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What is error Cannot find symbol?

Compiler Error: cannot find symbol A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.

What does Cannot resolve symbol mean in Java?

In JavaLanguage, if you get 'cannot resolve symbol', you have used a name that the compiler hasn't recognized. Class names -- If it is a class name, the compiler cannot find the class.


1 Answers

Change directories to the parent directory of assignment02. You should then be able to use

javac assignment02\Course.java assignment02\Offering.java

or

javac assignment02\Course.java 
javac assignment02\Offering.java

or even

javac assignment02\*.java

The compiler is is looking for the Course class in the assignment02 package FROM your current directory (so when you're in the assignment02 directory, it's effectively trying to look in assignment02/assignment02, which obviously isn't right).

While this will correct your current problem you will then get the following error:

assignment02\Offering.java:15: cannot find symbol
symbol  : variable getNumCredits
location: class assignment02.Course
    return course.getNumCredits;
                 ^
1 error

Which will need to corrected.

like image 96
MadProgrammer Avatar answered Oct 18 '22 18:10

MadProgrammer