Results 1 to 4 of 4
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default Need help with importing classes

    Hi,

    I'm a beginner to Java and I am studying how to program on my own. I have a problem importing custom classes into my code. Here is an example of the problem. I'm creating a simple name program that asks the user for their name, then shows the name in different formats such as the "first" + "last." I have two classes in separate java files, one called Name and another NewNameDriver. I try to import the Name class into NewNameDriver, which also has the main method, but when I try to compile NewNameDriver, the compile fails and shows that there is a problem with importing Name.

    Any help is greatly appreciated,

    Here is the code I use for file name Name.java:

    import java.io.*;

    class Name {
    String first;
    String last;
    String middle;

    public Name() throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter first name: ");
    first = in.readLine();
    System.out.print("Enter last name: ");
    last = in.readLine();
    System.out.print("Enter middle name: ");
    middle = in.readLine();
    }

    public Name(String firstName, String lastName, String middleName) {
    first = firstName;
    last = lastName;
    middle = middleName;
    }

    public String firstLast() {
    return first + " " + last;
    }

    public String full() {
    return first + " " + middle + " " + last;
    }

    public String lastFirstMI() {
    return last + ", " + first + ", " + middle.substring(0,1) + ".";
    }
    }


    Here is the code for NewNameDriver.java:

    import java.io.*;
    import Name;

    public class NewNameDriver {

    public static void main(String[] args) throws IOException {
    Name testName = new Name();
    System.out.println("Name in first-last format is" +
    testName.firstLast());
    System.out.println("Name in last-first-initial format is" +
    testName.lastFirstMI());
    }
    }

    Here is what I see when i try to compile the program:

    c:\Program Files\Java\jdk1.6.0_02\bin2>javac Name.java

    c:\Program Files\Java\jdk1.6.0_02\bin2>javac NewNameDriver.java
    NewNameDriver.java:2: ' .' expected
    import Name;

    1 error

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Packages

    Hello Deathmonger

    Organize your classes into packages. If you add them to the same package then you do not have to import your own classes. See this code:
    Name.java
    Java Code:
    package pack;
    
    import java.io.*;
    
    [COLOR="RoyalBlue"]public [/COLOR]class Name {
    	String first;
    	String last;
    	String middle;
    
    	public Name() throws IOException {
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print("Enter first name: ");
    		first = in.readLine();
    		System.out.print("Enter last name: ");
    		last = in.readLine();
    		System.out.print("Enter middle name: ");
    		middle = in.readLine();
    	}
    
    	public Name(String firstName, String lastName, String middleName) {
    		first = firstName;
    		last = lastName;
    		middle = middleName;
    	}
    
    	public String firstLast() {
    		return first + " " + last;
    	}
    
    	public String full() {
    		return first + " " + middle + " " + last;
    	}
    
    	public String lastFirstMI() {
    		return last + ", " + first + ", " + middle.substring(0,1) + ".";
    	}
    }
    NewNameDriver.java
    Java Code:
    package pack;
    
    import java.io.*;
    
    public class NewNameDriver {
    	public static void main(String[] args) throws IOException {
    		Name testName = new Name();
    		System.out.println("Name in first-last format is" +
    		testName.firstLast());
    		System.out.println("Name in last-first-initial format is" +
    		testName.lastFirstMI());
    	}
    }
    The Java source files need do be in a folder called according to your package. For my code, it would be a folder called "pack". Also, classes that do not have the public modifier cannot be used by other classes that are not part of the same package.

    Please use code tags when posting code. I hope this helped. ;)
    Last edited by tim; 02-06-2008 at 10:57 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default hi

    Hey Everybody I"m A Java Newbie Hope To Learn A Lot!!!!!!!!

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Welcome

    Welcome to the forums slamdunk6662003! ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Importing / compiling and running with .jar package
    By splinter64uk in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 03:47 AM
  2. Importing package
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 01:29 PM
  3. Importing classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 03:27 PM
  4. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 12:23 PM
  5. Exporting/Importing JAR files
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •