-3

I am new to OOP concepts, and I get an error: cannot find symbol. Also during compilation, i get the 2 errors:

Error:(9, 35) java: <identifier> expected
Error:(9, 36) java: illegal start of type

Any help would be greatly appreciated. Here are my two classes:

package com.company;

public class Main {

    public static void main(String[] args) {

    }

    TestClass waterBottle = new waterBottle();
    waterBottle.bottleFill(5);
}

package com.company;

public class TestClass {

    TestClass() { }

    public void waterBottleFill(int y) {
        int bottleFill = y;
        System.out.println("Fill level is at:" + bottleFill);
    }

    public void waterBottleRefill(int x) {
        int refill = x;
    }
}
specializt
  • 1,913
  • 15
  • 26

2 Answers2

1

Your code

TestClass waterBottle = new TestClass();
waterBottle.bottleFill(5);

is actually located in a class definition.

Move this code to your main method:

public class Main {

    public static void main(String[] args) {
        TestClass waterBottle = new waterBottle();
        waterBottle.bottleFill(5);
    }
}

This problem becomes obvious and plain when you format your code properly - with identation, vertical and horizontal spacing. I have edited your question, so that it is formatted properly now. Take a look and notice how readable it is now.

Another one problem is your class object creation. You have provided an incorrect name at new waterBottle() line. new should be followed by a proper class name:

TestClass waterBottle = new TestClass();
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0
package com.company;

public class Main {

public static void main(String[] args) {


    //TestClass waterBottle = new waterBottle(); // what is waterbottle.. this will give you can not find symbol`
    TestClass waterBottle = new TestClass();//should be this


        waterBottle.bottleFill(5);
}// main method ends

}// class ends




package com.company;

public class TestClass {

TestClass(){}

public void waterBottleFill(int y){
    int bottleFill = y;
    System.out.println("Fill level is at:"+ bottleFill);

}
public void waterBottleRefill(int x){
    int refill = x;
}


}
Nikhil
  • 300
  • 1
  • 5
  • 18