Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile error: cannot find symbol: In, StdIn and StdOut

Tags:

java

The code is from http://algs4.cs.princeton.edu/11model/BinarySearch.java.html for Algorithms textbook.

import java.util.Arrays;

public class BinarySearch {

    // precondition: array a[] is sorted
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] whitelist = In.readInts(args[0]);

        Arrays.sort(whitelist);

        // read key; print if not in whitelist
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (rank(key, whitelist) == -1)
                StdOut.println(key);
        }
    }
}

I get this error

$ javac BinarySearch.java 
BinarySearch.java:44: cannot find symbol
symbol  : variable In
location: class BinarySearch
        int[] whitelist = In.readInts(args[0]);
                          ^
BinarySearch.java:49: cannot find symbol
symbol  : variable StdIn
location: class BinarySearch
        while (!StdIn.isEmpty()) {
                ^
BinarySearch.java:50: cannot find symbol
symbol  : variable StdIn
location: class BinarySearch
            int key = StdIn.readInt();
                      ^
BinarySearch.java:52: cannot find symbol
symbol  : variable StdOut
location: class BinarySearch
                StdOut.println(key);
                ^
4 errors
like image 599
Jason Kim Avatar asked Apr 25 '13 06:04

Jason Kim


People also ask

How do I fix error Cannot find symbol?

In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What is meaning of error Cannot find symbol?

A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

How do you use StdIn and StdOut in Java?

To give input we use the input stream and to give output we use the output stream. One popular way to read input from stdin is by using the Scanner class and specifying the Input Stream as System.in. For example: Scanner scanner = new Scanner(System.in); String userString = scanner.


1 Answers

Classes StdIn, StdOut and In aren't part of the standard Java libraries. They're support classes provided to go along with the Princeton course.

From the 1.1 Programming Model page linked in the source code:

Standard input and standard output. StdIn.java and StdOut.java are libraries for reading in numbers and text from standard input and printing out numbers and text to standard output. Our versions have a simpler interface than the corresponding Java ones (and provide a few tecnical improvements).

...

In.java and Out.java are object-oriented versions that support multiple input and output streams, including reading from a file or URL and writing to a file.

So if you want to use the binary search code as-is, you'll need to download those files.

like image 175
Jon Skeet Avatar answered Oct 12 '22 22:10

Jon Skeet