Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Problem

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I'm new to Java (somewhat), and I'm running on a Java 2 Platform. I wrote the following class, and ran it:


class Box{


//instance variables
double width;
double length;
double height;


//constructors
Box(double w, double l, double h){
width = w;
length = l;
height = h;
}

Box(double side){
width = side;
length = side;
height = side;
}

//methods
public double volume(){
return (width * length * height);
}

public double area(){
return (2*(width * height + width *length + height * length));
}


} //class Box



Now I want to implement the following in another file:


import Box;
class BoxTester{

public static void main(String[] args){

Box box = new Box(2.5, 5.0, 6.0);

System.out.println("Area:" + box.area() + " volume: " + box.volume() );

System.out.println("length:" + box.length + " height: " + box.height +
"width: " + box.width );



}
} //class BoxTester



I always get the same thing when I try to compile BoxTester.java:


BoxTester.java:2: '.' expected
import Box;
^



I haven't put these into "packages" because I'm not that far on my training yet, but this should work, right?

I've tried variations like:
Import Box.class;

Import Box

Import Box.*;

And nothing works. Any help is appreciated.

Thanks,
scripter73


Change Your Thinking, Change Your Life.
 
Since SDK 1.4, you cannot import class names, only package names. Since your "Box" class is not in a a package, all you need to do is set your CLASSPATH to include the directory where the Box.class is exists, and omit the import statement from your code.
 
Hi sedj,

I had already set CLASSPATH when I setup the SDK. These two classes are in the same directory.

I removed the import statement and it worked like a charm!


Thanks,
scripter73


Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top