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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird problem 1

Status
Not open for further replies.

jisoo23

Programmer
Joined
Jan 27, 2004
Messages
192
Location
US
Hello all,

I have a very weird problem going on with a very simple program. Basically I'm using string tokenizer to run a java object in an infinite loop until you type in "Quit". The problem is that I get a compile time error for some reason that I cannot see. Everything looks good syntactically! I've tried putting them together in a package as well as integrating the main method into the test class itself but no dice. The error message I get is:

testdriver.java:16: cannot resolve symbol
symbol : class test
location: class testdriver
test t = new test();
^

My code for my two files (test.java and testdriver.java) are below. Any ideas would be wonderful!

Thanks,
Jisoo23

Code:
//test.java
import java.io.*;
import java.util.*;

public class test{

        String str;

        public test(){
                str = "Hello World";
        }

        public String getString(){
                return str;
        }

}


//testdriver.java
import java.io.*;
import java.util.*;

class testdriver{

        public testdriver(){}

        public static void main(String args[]){
                String line;
                String token;
                StringTokenizer st;
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

                try{
                        test t = new test();
                        line = in.readLine().toLowerCase();
                        st = new StringTokenizer(line);
                        token = st.nextToken();

                        while(!token.equals("quit")){
                            line = in.readLine().toLowerCase();
                            st = new StringTokenizer(line);
                            token = st.nextToken();
                        }
                }
                catch(Exception e){
                    System.err.println("test.java: " + e);
                }
        }
}
 
I put both in the same file.
Called it testdriver.java.
Removed the duplicated imports.
Changed testdriver to public - since it contains the main-method, and is called from outside the package (outside of everything).
Changed test to be nonpublic (only one public class per file).
Compiled.
Started.
Worked.

But: Make classname Uppercase (and filename accordingly).
Read the CodingStyles about that.
It's essential, if you read code, to distinguish fast: is it a Type, or is it a method?

If you put one into a package, you have to put both into packages. (Not necessarly the same package, but then 'Test' has to be public too.)
And package have to correspond to directories. This leads beginners into big, big trouble - never saw a commandline before.

 
I created a test.java file and a testdriver.java file (all lowercase) and compiled them with no problem.

If you're compiling from the command line, make sure you have the current directory in your classpath:

-classpath .;

 
Hello!

Thanks for the valuable input. I tested my code on another machine and it compiles and runs, which means that idarke probably has the right idea. I'm not quite sure how to set the current directory in the classpath though as I'm running Mandrake 9.2. Is there a different way of putting this into the profile file? Or is "-classpath .;" pretty much the same? This is what I have for my classpath so far:

export CLASSPATH=/usr/java/j2sdk1.4.2_04/lib/tools.jar:/usr/java/j2sdk1.4.2_04/jre/lib/rt.jar

Thanks,
Jisoo23
 
Nevermind, figured it out!

Thanks =)
Jisoo23
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top