简体   繁体   中英

Why am I getting “Cannot find symbol” error when I already defined the statements in another.java file?

Right now, I'm learning java before I start the semester so I can get the hang of it without struggling. At the moment, I'm having trouble compiling the current file I'm working on called RoomArea.java .

I get the error of not finding a symbol. For example:

cannot find symbol

symbol : method setLength

location: variable kitchen of type Rectangle

Here is a snippet of my Rectangle.java file:

package rectangle;   

public class Rectangle 
{
     private double length;
     private double width;

     public void setLength(double l)
     {
         length = l;        

     }

     public void setWidth(double w)
     {
         width = w;         
     }

}

Here's where I get the errors of calling the functions from Rectangle.java into my RoomAreas.java file:

package roomarea;

import java.awt.Rectangle;
import java.util.Scanner;

public class RoomArea 
{


public static void main(String[] args) 
{
  double number;
   double totalArea;

  Scanner keyboard = new Scanner(System.in);




  /* Creating three Rectangle object */
 Rectangle bedroom = new Rectangle();
 Rectangle kitchen = new Rectangle();
 Rectangle den =  new Rectangle();

 /* Get and store the dimensions of the kitchen */
 System.out.print("What is the kitchen's length?");
 number = keyboard.nextDouble();
 kitchen.setLength(number);
 System.out.print("What is the kitchen's width? ");
 number = keyboard.nextDouble();
 kitchen.setWidth(number);

// ...
// ...
}

You're importing the wrong Rectangle class in RoomArea :

import java.awt.Rectangle;

should be

import rectangle.Rectangle;

(assuming you're actually trying to use your own Rectangle class).

You are not using your own Rectangle class. Your are importing java.awt.Rectangle . Java will now interprete your three Rectangles as java.awt.Rectangle Objects.

Change

import Java.awt.Rectangle;

to

import rectangle.Rectangle;

and it should work.

found these issues with your code. 1. import proper rectangle 2. whatever you have pasted roomarea.java , needs one more "}" at the end.

Now compile class Rectangle.java from package rectangle and compile roomarea.java from roomarea like javac -cp ../ roomarea.java. this would create class files in respective folders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM