Link to home
Start Free TrialLog in
Avatar of kellyputty
kellyputty

asked on

Can't compile these two files, why?

Here are the following two files I can't get to compile with javac.
Both files are part of package com.bruceeckel.simpletest
all other files in com.bruceeckel.simpletest are compiled.
Each of these two files seems to depend on the other. (The two files are OutputVerifier.java (file 1) & Test.java (file 2))
I place the errors after each respective file.
------------------------------------file 1-------------------------------------------------
//: com:bruceeckel:simpletest:OutputVerifier.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.simpletest;
import java.util.*;
import java.io.PrintStream;

public class OutputVerifier {
  private static void verifyLength(
    int output, int expected, int compare) {
    if((compare == Test.EXACT && expected != output)
      || (compare == Test.AT_LEAST && output < expected))
      throw new NumOfLinesException(expected, output);
  }
  public static void verify(List output, List expected) {
    verifyLength(output.size(),expected.size(),Test.EXACT);
    if(!expected.equals(output)) {
      //find the line of mismatch
      ListIterator it1 = expected.listIterator();
      ListIterator it2 = output.listIterator();
      while(it1.hasNext()
        && it2.hasNext()
        && it1.next().equals(it2.next()));
      throw new LineMismatchException(
        it1.nextIndex(), it1.previous().toString(),
        it2.previous().toString());
    }
  }
  public static void
  verifyIgnoreOrder(List output, Object[] expected) {
    verifyLength(expected.length,output.size(),Test.EXACT);
    if(!(expected instanceof String[]))
      throw new RuntimeException(
        "IGNORE_ORDER only works with String objects");
    String[] out = new String[output.size()];
    Iterator it = output.iterator();
    for(int i = 0; i < out.length; i++)
      out[i] = it.next().toString();
    Arrays.sort(out);
    Arrays.sort(expected);
    int i =0;
    if(!Arrays.equals(expected, out)) {
      while(expected[i].equals(out[i])) {i++;}
      throw new SimpleTestException(
        ((String) out[i]).compareTo(expected[i]) < 0
          ? "output: <" + out[i] + ">"
          : "expected: <" + expected[i] + ">");
    }
  }
  public static void
  verifyAtLeast(List output, List expected) {
    verifyLength(output.size(), expected.size(),
      Test.AT_LEAST);
    if(!output.containsAll(expected)) {
      ListIterator it = expected.listIterator();
      while(output.contains(it.next())) {}
      throw new SimpleTestException(
        "expected: <" + it.previous().toString() + ">");
    }
  }
} ///:~

-----------------------------end file 1---------------------------------
----------------------------javac errors file 1-----------------------
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Kelly>cd ../../java/code

C:\java\code>cd com/bruceeckel/simpletest

C:\java\code\com\bruceeckel\simpletest>javac OutputVerifier.java
OutputVerifier.java:11: cannot find symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    if((compare == Test.EXACT && expected != output)
                   ^
OutputVerifier.java:12: cannot find symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
      || (compare == Test.AT_LEAST && output < expected))
                     ^
OutputVerifier.java:13: cannot find symbol
symbol  : class NumOfLinesException
location: class com.bruceeckel.simpletest.OutputVerifier
      throw new NumOfLinesException(expected, output);
                ^
OutputVerifier.java:16: cannot find symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    verifyLength(output.size(),expected.size(),Test.EXACT);
                                               ^
OutputVerifier.java:24: cannot find symbol
symbol  : class LineMismatchException
location: class com.bruceeckel.simpletest.OutputVerifier
      throw new LineMismatchException(
                ^
OutputVerifier.java:31: cannot find symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    verifyLength(expected.length,output.size(),Test.EXACT);
                                               ^
OutputVerifier.java:44: cannot find symbol
symbol  : class SimpleTestException
location: class com.bruceeckel.simpletest.OutputVerifier
      throw new SimpleTestException(
                ^
OutputVerifier.java:45: compareTo(java.lang.String) in java.lang.String cannot b
e applied to (java.lang.Object)
        ((String) out[i]).compareTo(expected[i]) < 0
        ^
OutputVerifier.java:53: cannot find symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
      Test.AT_LEAST);
      ^
OutputVerifier.java:57: cannot find symbol
symbol  : class SimpleTestException
location: class com.bruceeckel.simpletest.OutputVerifier
      throw new SimpleTestException(
                ^
Note: OutputVerifier.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors
----------------------------end javac errors file 1-----------------

------------------------------file 2------------------------------------
//: com:bruceeckel:simpletest:Test.java
// Simple utility for testing program output. Intercepts
// System.out to print both to the console and a buffer.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.simpletest;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Test {
  // Bit-shifted so they can be added together:
  public static final int
    EXACT = 1 << 0, // Lines must match exactly
    AT_LEAST = 1 << 1, // Must be at least these lines
    IGNORE_ORDER = 1 << 2, // Ignore line order
    WAIT = 1 << 3; // Delay until all lines are output
  private String className;
  private TestStream testStream;
  public Test() {
    // Discover the name of the class this
    // object was created within:
    className =
      new Throwable().getStackTrace()[1].getClassName();
    testStream = new TestStream(className);
  }
  public static List fileToList(String fname) {
    ArrayList list = new ArrayList();
    try {
      BufferedReader in =
        new BufferedReader(new FileReader(fname));
      try {
        String line;
        while((line = in.readLine()) != null) {
          if(fname.endsWith(".txt"))
            list.add(line);
          else
            list.add(new TestExpression(line));
        }
      } finally {
        in.close();
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return list;
  }
  public static List arrayToList(Object[] array) {
    List l = new ArrayList();
    for(int i = 0; i < array.length; i++) {
      if(array[i] instanceof TestExpression) {
        TestExpression re = (TestExpression)array[i];
        for(int j = 0; j < re.getNumber(); j++)
          l.add(re);
      } else {
        l.add(new TestExpression(array[i].toString()));
      }
    }
    return l;
  }
  public void expect(Object[] exp, int flags) {
    if((flags & WAIT) != 0)
      while(testStream.numOfLines < exp.length) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
      List output = fileToList(className + "Output.txt");
      if((flags & IGNORE_ORDER) == IGNORE_ORDER)
        OutputVerifier.verifyIgnoreOrder(output, exp);
      else if((flags & AT_LEAST) == AT_LEAST)
        OutputVerifier.verifyAtLeast(output,
          arrayToList(exp));
      else
        OutputVerifier.verify(output, arrayToList(exp));
    // Clean up the output file - see c06:Detergent.java
    testStream.openOutputFile();
  }
  public void expect(Object[] expected) {
    expect(expected, EXACT);
  }
  public void expect(Object[] expectFirst,
    String fname, int flags) {
    List expected = fileToList(fname);
    for(int i = 0; i < expectFirst.length; i++)
      expected.add(i, expectFirst[i]);
    expect(expected.toArray(), flags);
  }
  public void expect(Object[] expectFirst, String fname) {
    expect(expectFirst, fname, EXACT);
  }
  public void expect(String fname) {
    expect(new Object[] {}, fname, EXACT);
  }
} ///:~


-----------------------------end file 2---------------------------
---------------------------javac errors file 2-------------------
C:\java\code\com\bruceeckel\simpletest>javac Test.java
Test.java:19: cannot find symbol
symbol  : class TestStream
location: class com.bruceeckel.simpletest.Test
  private TestStream testStream;
          ^
Test.java:25: cannot find symbol
symbol  : class TestStream
location: class com.bruceeckel.simpletest.Test
    testStream = new TestStream(className);
                     ^
Test.java:38: cannot find symbol
symbol  : class TestExpression
location: class com.bruceeckel.simpletest.Test
            list.add(new TestExpression(line));
                         ^
Test.java:51: cannot find symbol
symbol  : class TestExpression
location: class com.bruceeckel.simpletest.Test
      if(array[i] instanceof TestExpression) {
                             ^
Test.java:52: cannot find symbol
symbol  : class TestExpression
location: class com.bruceeckel.simpletest.Test
        TestExpression re = (TestExpression)array[i];
        ^
Test.java:52: cannot find symbol
symbol  : class TestExpression
location: class com.bruceeckel.simpletest.Test
        TestExpression re = (TestExpression)array[i];
                             ^
Test.java:56: cannot find symbol
symbol  : class TestExpression
location: class com.bruceeckel.simpletest.Test
        l.add(new TestExpression(array[i].toString()));
                  ^
Test.java:72: cannot find symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verifyIgnoreOrder(output, exp);
        ^
Test.java:74: cannot find symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verifyAtLeast(output,
        ^
Test.java:77: cannot find symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verify(output, arrayToList(exp));
        ^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors
-------------------endjavac errors file 2-----------------
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

C:\java\code>java com\bruceeckel\simpletest\*.java
Avatar of kellyputty
kellyputty

ASKER


Did you mean javac instead of java?
check if TestStream.class is there in the same folder 1st & also all the other classes which you are using in the programs.
>>Did you mean javac instead of java?

LOL - yes sorry


C:\java\code>javac com\bruceeckel\simpletest\*.java
com\bruceeckel\simpletest\OutputVerifier.java:45: compareTo(java.lang.String) in
 java.lang.String cannot be applied to (java.lang.Object)
        ((String) out[i]).compareTo(expected[i]) < 0
        ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

C:\java\code>
Yes TestStream and all others are in the SimpleTest and compile fine.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am afraid to change any code.  These files are from my test book.  I am supposed to import them into other example source code provided by the text in order to practice.

none of this is my code.

(Thinking in Java.  Bruce Eckel version 3)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes that is the only error based on CHEJ's suggestion, I will go in and change the code.  I will let you know what happens...
They now compile.
Thank you.
:-)