#Beginner, Cannot Find Symbol Error

65 messages ยท Page 1 of 1 (latest)

crystal adder
#

I have a Main class which accesses a method from the FibonacciCalculator class in another file, yet keeps giving me this error:
Main.java:6: error: cannot find symbol
FibonacciCalculator calculator = new FibonacciCalculator();
^
symbol: class FibonacciCalculator
location: class Main
Main.java:6: error: cannot find symbol
FibonacciCalculator calculator = new FibonacciCalculator();
^
symbol: class FibonacciCalculator
location: class Main
2 errors

Here are the two files:
Main.java:

package CS200Projects;    

public class Main {
    public static void main(String[] args) {
        FibonacciCalculator calculator = new FibonacciCalculator();
        int result = calculator.calculateFibonacci(10);
        System.out.println("The 10th Fibonacci number is: " + result);
    }
}

FibonacciCalculator.java:

package CS200Projects;

public class FibonacciCalculator {
    public int calculateFibonacci(int n) {
        if (n == 0 || n == 1) {
            return n;
        }
        return calculateFibonacci(n - 1) + calculateFibonacci(n - 2);
    }
}
snow axleBOT
#

โŒ› This post has been reserved for your question.

Hey @crystal adder! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

crystal adder
#

I did the package thing at the top, does that not do it?

#

Sorry this is my first time ever using Java, used to Python :(

chilly spruce
#

it's okay, the package is a qualifier for the current package. if you want to use code from another package, you need to import it using the import keyword.

#

if you're using an IDE you can hover over the error and it should say add import statement or similar

crystal adder
#

import CS200Projects.FibonacciCalculator;
Put this below the package then on my Main.java file?

chilly spruce
#

wait your classes seem to be in the same package

crystal adder
#

Yeah

crystal adder
chilly spruce
#

did you save your code?

#

or is it working now?

#

maybe I forget how packages work lol

crystal adder
#

No it's still not working, just saved

chilly spruce
#

oh ok

crystal adder
#

This is the new error after adding the import:
Main.java:2: error: cannot find symbol
import CS200Projects.FibonacciCalculator;
^
symbol: class FibonacciCalculator
location: package CS200Projects
Main.java:7: error: cannot find symbol
FibonacciCalculator calculator = new FibonacciCalculator();
^
symbol: class FibonacciCalculator
location: class Main
Main.java:7: error: cannot find symbol
FibonacciCalculator calculator = new FibonacciCalculator();
^
symbol: class FibonacciCalculator
location: class Main
3 errors

#

It got angrier ๐Ÿ’€

chilly spruce
#

yeah the import statement isn't necessary since they are in the same package

crystal adder
#

Right right

chilly spruce
#

could you share your project structure?

crystal adder
#

Like this?

#

It's just these two

#

Sorry did you mean something else

chilly spruce
#

try getting rid of the package statement

crystal adder
#

@chilly spruce Any ideas...?

crystal adder
#

Can anyone else help with this?

chilly spruce
#

is it still showing the same error?

#

with the packages, even after saving?

crystal adder
#

Yeah

#

With package, saved

chilly spruce
#

you want to run it with VSCode

crystal adder
#

I'm using vscode

chilly spruce
#

but how are you running it?

crystal adder
#

Clicking run code

#

Should I be doing it another way?

chilly spruce
crystal adder
#

Yep

#

That's how I'm doing it

chilly spruce
# chilly spruce

and your project structure is exactly like mine, one folder and two source files? that it?

crystal adder
#

Yep

chilly spruce
#

what extensions are you using?

crystal adder
#

idk what that .cph is though, wasn't there before...

chilly spruce
crystal adder
#

yeah

#

Doxxed but who cares ๐Ÿ’€

#

Lemme know if you can access that

chilly spruce
#

I'm just going to clone it

crystal adder
#

Yeah

#

Extensions for Java are just these btw

chilly spruce
#

try starting over. clone it and open the root in VS Code

#

all I did was clone it and ran it without issue.

crystal adder
#

very strange

crystal adder
#

The default for the button was run code

#

I had to switch it to run java

#

Thanks for the help tho :)

snow axleBOT
# crystal adder Thanks for the help tho :)

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.