0

How do I fix a "Cannot find symbol" error? I've started the Java tutorials yesterday in I've made this:

class BicycleDemo {
public static void main(String[] args) {

    // Create two different 
    // Bicycle objects
    Bicycle bike1 = new Bicycle();
    Bicycle bike2 = new Bicycle();

    // Invoke methods on 
    // those objects
    bike1.changeCadence(50);
    bike1.speedUp(10);
    bike1.changeGear(2);
    bike1.printStates();

    bike2.changeCadence(50);
    bike2.speedUp(10);
    bike2.changeGear(2);
    bike2.changeCadence(40);
    bike2.speedUp(10);
    bike2.changeGear(3);
    bike2.printStates();
}
}

At the first line, it says "incorrect package" (Which is wierd, because packages is the next chapter in the tutorial) At line 6 and 7 (Bicycle bike 1 and 2), it says "Cannot find symbol".

Also, there is this:

class Bicycle {

int cadence = 0;
int speed = 0;
int gear = 1;

void changeCadence(int newValue) {
     cadence = newValue;
}

void changeGear(int newValue) {
     gear = newValue;
}

void speedUp(int increment) {
     speed = speed + increment;   
}

void applyBrakes(int decrement) {
     speed = speed - decrement;
}

void printStates() {
     System.out.println("cadence:" +
         cadence + " speed:" + 
         speed + " gear:" + gear);
}
}

But I have no idea how to make them work together, if they're supposed to. These things are probably really easy to fix, but obviously, I have next to no knowledge about this yet.

msrd0
  • 7,816
  • 9
  • 47
  • 82
Jerrit
  • 119
  • 1
  • 2
  • 10
  • Have you imported `Bicycle` class in your `BicycleDemo` class? – Pradeep Simha Jan 17 '15 at 09:50
  • And how did you attempt to run the compiler? – Patricia Shanahan Jan 17 '15 at 09:51
  • 3
    If it says "incorrect package" that means that your classes `package` declaration does not match the `.java` file location. In your case, you have no `package` declaration, so you classes are in the **default** package. This means that the files should be at the root of the classpath. – Boris the Spider Jan 17 '15 at 09:52
  • @PradeepSimha I've tried, but that didnt work properly... – Jerrit Jan 18 '15 at 10:46
  • @PatriciaShanahan Just build and then run :S – Jerrit Jan 18 '15 at 10:46
  • @BoristheSpider Okay, so put it in the Bicycle package, for example, would fix it? I cant check now because im not home. – Jerrit Jan 18 '15 at 10:47
  • @Jack I was looking for much more detail. Some ways of "just build it" work. Others will not. Where you building from the command line? If so, what did you type? From an IDE? What project set-up.... – Patricia Shanahan Jan 18 '15 at 11:47
  • I have solved it, thanks to @BoristheSpider. I think I simply didn't delcare the package. The thing is, I didn't fully understand what they meant my declaring things, now I do, so yeah... Thanks everyone! – Jerrit Jan 19 '15 at 12:55

1 Answers1

0
  1. keep both all your files in some place
  2. remove all packages or create a new project
  3. create new packages as you like
  4. first create the Bicycle class
  5. then create your other classes by pasting the Bicycle class name
  6. then it will update your imports currect

yes I know it's a mess, but sometimes it's the best option

azro
  • 53,056
  • 7
  • 34
  • 70
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • Yes, I have deleted the entire project, created Bicycle, then made BicycleDemo under the same package name, although in a different class, ran it, and the output was correct! I think the problem was simply that I kept forgetting to declare the package. – Jerrit Jan 19 '15 at 12:54
  • ok, you can contact me trough my private web-site and i'll guide you trough remote if you want – roeygol Jan 19 '15 at 14:03