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: OutputVeri fier.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(expect ed, output);
}
public static void verify(List output, List expected) {
verifyLength(output.size() ,expected. size(),Tes t.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.leng th,output. size(),Tes t.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(o ut[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(exp ected)) {
ListIterator it = expected.listIterator();
while(output.contains(it.n ext())) {}
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\bruceecke l\simplete st>javac OutputVerifier.java
OutputVerifier.java:11: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest. OutputVeri fier
if((compare == Test.EXACT && expected != output)
^
OutputVerifier.java:12: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest. OutputVeri fier
|| (compare == Test.AT_LEAST && output < expected))
^
OutputVerifier.java:13: cannot find symbol
symbol : class NumOfLinesException
location: class com.bruceeckel.simpletest. OutputVeri fier
throw new NumOfLinesException(expect ed, output);
^
OutputVerifier.java:16: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest. OutputVeri fier
verifyLength(output.size() ,expected. size(),Tes t.EXACT);
^
OutputVerifier.java:24: cannot find symbol
symbol : class LineMismatchException
location: class com.bruceeckel.simpletest. OutputVeri fier
throw new LineMismatchException(
^
OutputVerifier.java:31: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest. OutputVeri fier
verifyLength(expected.leng th,output. size(),Tes t.EXACT);
^
OutputVerifier.java:44: cannot find symbol
symbol : class SimpleTestException
location: class com.bruceeckel.simpletest. OutputVeri fier
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. OutputVeri fier
Test.AT_LEAST);
^
OutputVerifier.java:57: cannot find symbol
symbol : class SimpleTestException
location: class com.bruceeckel.simpletest. OutputVeri fier
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].getCl assName();
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].to String())) ;
}
}
return l;
}
public void expect(Object[] exp, int flags) {
if((flags & WAIT) != 0)
while(testStream.numOfLine s < 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.verifyIgnor eOrder(out put, exp);
else if((flags & AT_LEAST) == AT_LEAST)
OutputVerifier.verifyAtLea st(output,
arrayToList(exp));
else
OutputVerifier.verify(outp ut, 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\bruceecke l\simplete st>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].to String())) ;
^
Test.java:72: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest. Test
OutputVerifier.verifyIgnor eOrder(out put, exp);
^
Test.java:74: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest. Test
OutputVerifier.verifyAtLea st(output,
^
Test.java:77: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest. Test
OutputVerifier.verify(outp ut, arrayToList(exp));
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors
-------------------endjava c errors file 2-----------------
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.
--------------------------
//: com:bruceeckel:simpletest:
// 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(expect
}
public static void verify(List output, List expected) {
verifyLength(output.size()
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.leng
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
while(expected[i].equals(o
throw new SimpleTestException(
((String) out[i]).compareTo(expected
? "output: <" + out[i] + ">"
: "expected: <" + expected[i] + ">");
}
}
public static void
verifyAtLeast(List output, List expected) {
verifyLength(output.size()
Test.AT_LEAST);
if(!output.containsAll(exp
ListIterator it = expected.listIterator();
while(output.contains(it.n
throw new SimpleTestException(
"expected: <" + it.previous().toString() + ">");
}
}
} ///:~
--------------------------
--------------------------
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\bruceecke
OutputVerifier.java:11: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest.
if((compare == Test.EXACT && expected != output)
^
OutputVerifier.java:12: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest.
|| (compare == Test.AT_LEAST && output < expected))
^
OutputVerifier.java:13: cannot find symbol
symbol : class NumOfLinesException
location: class com.bruceeckel.simpletest.
throw new NumOfLinesException(expect
^
OutputVerifier.java:16: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest.
verifyLength(output.size()
^
OutputVerifier.java:24: cannot find symbol
symbol : class LineMismatchException
location: class com.bruceeckel.simpletest.
throw new LineMismatchException(
^
OutputVerifier.java:31: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest.
verifyLength(expected.leng
^
OutputVerifier.java:44: cannot find symbol
symbol : class SimpleTestException
location: class com.bruceeckel.simpletest.
throw new SimpleTestException(
^
OutputVerifier.java:45: compareTo(java.lang.String
e applied to (java.lang.Object)
((String) out[i]).compareTo(expected
^
OutputVerifier.java:53: cannot find symbol
symbol : variable Test
location: class com.bruceeckel.simpletest.
Test.AT_LEAST);
^
OutputVerifier.java:57: cannot find symbol
symbol : class SimpleTestException
location: class com.bruceeckel.simpletest.
throw new SimpleTestException(
^
Note: OutputVerifier.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors
--------------------------
--------------------------
//: com:bruceeckel:simpletest:
// 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(
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].to
}
}
return l;
}
public void expect(Object[] exp, int flags) {
if((flags & WAIT) != 0)
while(testStream.numOfLine
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
List output = fileToList(className + "Output.txt");
if((flags & IGNORE_ORDER) == IGNORE_ORDER)
OutputVerifier.verifyIgnor
else if((flags & AT_LEAST) == AT_LEAST)
OutputVerifier.verifyAtLea
arrayToList(exp));
else
OutputVerifier.verify(outp
// 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(),
}
public void expect(Object[] expectFirst, String fname) {
expect(expectFirst, fname, EXACT);
}
public void expect(String fname) {
expect(new Object[] {}, fname, EXACT);
}
} ///:~
--------------------------
--------------------------
C:\java\code\com\bruceecke
Test.java:19: cannot find symbol
symbol : class TestStream
location: class com.bruceeckel.simpletest.
private TestStream testStream;
^
Test.java:25: cannot find symbol
symbol : class TestStream
location: class com.bruceeckel.simpletest.
testStream = new TestStream(className);
^
Test.java:38: cannot find symbol
symbol : class TestExpression
location: class com.bruceeckel.simpletest.
list.add(new TestExpression(line));
^
Test.java:51: cannot find symbol
symbol : class TestExpression
location: class com.bruceeckel.simpletest.
if(array[i] instanceof TestExpression) {
^
Test.java:52: cannot find symbol
symbol : class TestExpression
location: class com.bruceeckel.simpletest.
TestExpression re = (TestExpression)array[i];
^
Test.java:52: cannot find symbol
symbol : class TestExpression
location: class com.bruceeckel.simpletest.
TestExpression re = (TestExpression)array[i];
^
Test.java:56: cannot find symbol
symbol : class TestExpression
location: class com.bruceeckel.simpletest.
l.add(new TestExpression(array[i].to
^
Test.java:72: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest.
OutputVerifier.verifyIgnor
^
Test.java:74: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest.
OutputVerifier.verifyAtLea
^
Test.java:77: cannot find symbol
symbol : variable OutputVerifier
location: class com.bruceeckel.simpletest.
OutputVerifier.verify(outp
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors
-------------------endjava
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
LOL - yes sorry
ASKER
C:\java\code>javac com\bruceeckel\simpletest\
com\bruceeckel\simpletest\
java.lang.String cannot be applied to (java.lang.Object)
((String) out[i]).compareTo(expected
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
C:\java\code>
ASKER
Yes TestStream and all others are in the SimpleTest and compile fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
none of this is my code.
(Thinking in Java. Bruce Eckel version 3)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
ASKER
They now compile.
Thank you.
Thank you.
:-)
C:\java\code>java com\bruceeckel\simpletest\