0

I have two files: MyStringLog.java (which extends ArrayStringLog) and TestDriver.java in the same directory. Why doesn't TestDriver.java recognize the MyStringLog constructor from the MyStringLog class? In TestDriver.java I declare:

MyStringLog animals = new MyStringLog("animals");

This is supposed to construct a new MyStringLog object named animals, but I get 2 errors when I compile, both, that MyStringLog symbol is not found.

Van
  • 169
  • 1
  • 3
  • 11

2 Answers2

1

I got this to roughly work. To do so...

for the file ArrayStringLog.java, I removed the implements StringLogInterface because I simply don't have access to that interface. After doing so, I removed the package ch02.stringLogs;. When I compiled this file with the package... still there, I recieved the error: Exception in thread "main" java.lang.NoClassDefFoundError: ArrayStringLog (wrong name: ch02/stringLogs/ArrayStringLog)

Then in MyStringLog.java, I removed the import ch02.stringLogs.*;. I then saved and compiled the code, and ran the TestDriver, to which I received no compilation errors.

This leads me to believe that your error stems from the package statement in ArrayStringLog.java.


To finally get a compilation, I put all four files (ArrayStringLog, MyStringLog, StringLogInterface, TestDriver) into the same directory, removed any package... statements, added back implements StringLogInterface to ArrayStringLog.java, compiled each one, and then ran TestDriver with an added toString method from which the output was:

Log: animals

1. dog
2. cat

Here was the test driver:

public class TestDriver {
    public static void main(String[] args) {
        MyStringLog animals = new MyStringLog("animals");
        animals.insert("dog");
        animals.insert("cat");

        System.out.println(animals.toString());
    }
}

To make clear, ArrayStringLog begins with:

public class ArrayStringLog implements StringLogInterface
Friendly King
  • 2,396
  • 1
  • 23
  • 40
  • Whoops, that was a typo posting it here. My file there is no typo. – Van Sep 11 '13 at 01:03
  • 1
    Note, the above comment was in response to my previous answer and no longer applies. – Friendly King Sep 11 '13 at 01:24
  • https://www.dropbox.com/s/6koo8eim16xcskb/bookFiles.zip these are all the files we're using. I looked at a classmate's code which compiled and they didn't have to touch ArrayStringLog.java. – Van Sep 11 '13 at 01:40
  • See my revised answer. If you have more questions, comment away. – Friendly King Sep 11 '13 at 01:48
  • I removed "implements StringLogInterface" from ArrayStringLog.java. I removed "package ch02.stringLogs;" from all files. I placed them all in the same directory, but MyStringLog.java won't compile and neither will TestDriver.java. Did I forget to remove something? – Van Sep 11 '13 at 02:13
  • How about now (see the bolded edit)? I think I miscommunicated a bit. My fault. – Friendly King Sep 11 '13 at 02:25
  • I got it to work by opening a new Terminal which reset $CLASSPATH. Thanks for all of your awesome help. – Van Sep 11 '13 at 02:26
0

try adding import MyStringLog; to your file.

Also are you using packages? In which case it should be import <package_name>.MyStringLog;

vandale
  • 3,600
  • 3
  • 22
  • 39
  • This gives me the error "TestDriver.java:1: error: '.' expected import MyStringLog;" with the arrow pointing to the semicolon. The ArrayStringLog class is in the ch02.stringLogs package, which MyStringLog.java imports. – Van Sep 11 '13 at 01:14
  • use `import ch02.stringLogs.MyStringLog;` then – vandale Sep 11 '13 at 01:21