Some Java help please

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,303
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
I'm finally fed up with Java. We got assigned our first independent programming project today and I built a program that would satisfy the assignment. However, I'm building a much larger more complex program just because. It refuses to work. I've created my own class, and when I try importing it, I always get a package doesn't exist compiler exception.

Here's the class I'm trying to import.
Code:
package conv;
 
public class Kgconverter
{
   private float kg;
   private float lb;
 
   public float convert(float kg)
   {
     lb = kg * 0.454F;
   }
}

And here's themain program:
Code:
import java.util.Scanner;
import conv.Kgconverter;
 
public class KilogramstoPounds
{
  public static void main(String[] args)
  {
     float kilograms;
     float pounds;
     Scanner input = new Scanner(System.in);
     Kgconverter conversion = new Kgconverter();
 
     kilogram = input.nextInt();
 
     pounds = conversion.convert(kilograms);
 
     System.out.println(kilograms + " Kilograms = " + pounds + "pounds");
   }
}
I've got the classpath stuff all set up in Vista and my IDE, in this case, JGrasp. Any idea what's causing the IDE not to find the class?
Thanks,
Zat
 
If you are getting a "package does not exist" error, it means that compiler cannot find the Kgconverter class in the conv package. With Java, there are two rules regarding packages:

1. Each non-inner class must reside in a filename that matches the class name.
2. Each class source file must reside in a subdirectory of the classpath that matches the package name of the class contained in that file.

In the case of your 'Kgconverter' class, that means that the source file in which that class resides must be named 'Kgconverter.java' and that Java file must reside in a subdirectory named 'conv'. In other words, if your classpath is set to this:

CLASSPATH=C:\myclasses

..then your Kgconverter code must reside in this file:

C:\myclasses\conv\Kgconverter.java


Remember to set your classpath to the top-level class directory (e.g., C:\myclasses), because all package names are relative to the package root directory.
 
Back
Top