Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2008, 10:22 PM
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 11:53 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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
Code:
package pack; import java.io.*; public 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
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.
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 02-06-2008 at 11:57 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-07-2008, 01:38 AM
Member
 
Join Date: Feb 2008
Posts: 1
slamdunk6662003 is on a distinguished road
hi
Hey Everybody I"m A Java Newbie Hope To Learn A Lot!!!!!!!!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-07-2008, 12:03 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Welcome
Welcome to the forums slamdunk6662003!
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing / compiling and running with .jar package splinter64uk New To Java 1 12-05-2007 04:47 AM
Importing package bugger New To Java 5 11-26-2007 02:29 PM
Importing classes Java Tip Java Tips 0 11-06-2007 04:27 PM
Importing a Custom Class jfredrickson New To Java 3 07-11-2007 01:23 PM
Exporting/Importing JAR files JavaForums Eclipse 0 04-26-2007 12:15 PM


All times are GMT +3. The time now is 04:53 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org