?
Solved

Can't compile these two files, why?

Posted on 2006-04-11
12
Medium Priority
?
328 Views
Last Modified: 2008-02-01
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-----------------
0
Comment
Question by:kellyputty
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16428978
Try

C:\java\code>java com\bruceeckel\simpletest\*.java
0
 

Author Comment

by:kellyputty
ID: 16429062

Did you mean javac instead of java?
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16429077
check if TestStream.class is there in the same folder 1st & also all the other classes which you are using in the programs.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16429082
>>Did you mean javac instead of java?

LOL - yes sorry
0
 

Author Comment

by:kellyputty
ID: 16429087


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>
0
 

Author Comment

by:kellyputty
ID: 16429112
Yes TestStream and all others are in the SimpleTest and compile fine.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1800 total points
ID: 16429119
Should probably be


((String) out[i]).compareTo((String)expected[i]) < 0
0
 

Author Comment

by:kellyputty
ID: 16429151
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)
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 200 total points
ID: 16429158
yes i this what CHEJ said should solve it. is  tht the only error you have???
0
 

Author Comment

by:kellyputty
ID: 16429235
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...
0
 

Author Comment

by:kellyputty
ID: 16429355
They now compile.
Thank you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16429589
:-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question