19

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
Jason Kim
  • 18,102
  • 13
  • 66
  • 105

7 Answers7

35

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.

EvgenyV
  • 1,226
  • 14
  • 28
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is it really directly use full for our program ? Please help me . – JDGuide Apr 25 '13 at 06:07
  • 1
    @JDeveloper: I don't understand your question completely (it's not clear), but you can use those classes yourself for working with `BinarySearch.java`, yes. See the licence terms if you want to use them elsewhere. – Jon Skeet Apr 25 '13 at 06:17
  • Thanks @JonSkeet! I basically downloaded In, StdIn and StdOut java files in the same directory and it worked out. – Jason Kim Apr 25 '13 at 06:51
  • Updated 11.09.2017 - [Q&A](https://introcs.cs.princeton.edu/java/stdlib/) section there is [stdlib-package.jar](https://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar) named package packaged version file that can be used for reference – NexusStar Dec 01 '17 at 22:30
9

If you have already set up the environment recommended for the course, only add this lines to your java file

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
lohenzoo
  • 145
  • 2
  • 7
  • I did this using DrJava and the program compiled. Thanks. My question is how do you include the edu..princeton.cs.algs4 package when using eclipse? I could not get it to compile using eclipse – Yonatan Simson Jan 27 '16 at 03:37
  • 1
    @YonatanSimson In the _Troubleshooting_ section of [the instruction page](https://algs4.cs.princeton.edu/mac/), it says that _**Can I use a different IDE?** Yes you can use another IDE (such as Eclipse) but you will have to configure the IDE properties yourself (such as the classpath)._ Thus I suggest you compile the file in terminal since it would be more convenient (only if you have installed the DrJava through `algs4.zip`) – YC_ Jun 12 '18 at 08:39
  • This is the solution that worked for me when using the latest method outlined in the algs4 website which is with the IntelliJ IDE. – ultrasounder Jan 05 '19 at 17:20
4

You can replace then with

Output:

 System.out.println(key);

Input

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String Key= null;
try {
     Key = reader.readLine();
} catch (IOException e) {
   e.printStackTrace();
} 

They are using custom libraries to print the value to console(Presumably) You can redirect the output to console.

Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55
4

StdIn and In are custom Libraries that are included within the algs4 class download. Execute the program with the command java-algs4 instead of just java and it should work.

Gladstone24
  • 318
  • 3
  • 10
0

I'm trying to use StdIn and StdOut by including stdlib.jar in my project and it is properly placed in the classpath, but I cannot make it work by adding:

  • import edu.princeton.cs.algs4.StdIn;
  • import edu.princeton.cs.algs4.StdOut;

in my own java file.

I find the solution here. I just removed package info in my java files and without any import info I can use StdIn and StdOut in my project since all my java files and the StdIn, StdOut are all in the default package.

By the way, It is a bad practice to do so in a real-world production-ready project.

Lebecca
  • 2,406
  • 15
  • 32
0

The stdlib.jar is better to be placed in the src folder for ease of finding the path. If you are using any IDE, please add the classpath of the library.

If you are confronted with the problem that the stdlib is the default library, please use these commands to compile and run the codes.

Compile

javac -cp .;<YOUR\PATH\OF\THE\LIB\stdlib.jar> yourProgram.java

Run

java -cp .;<YOUR\PATH\OF\THE\LIB\stdlib.jar> yourProgram <arguments>

If you follow my suggestion, then the path will be only stdlib.jar or more organised lib\stdlib.jar.

Ref link: https://introcs.cs.princeton.edu/java/stdlib/

4b0
  • 21,981
  • 30
  • 95
  • 142
Fengyi Li
  • 46
  • 4
-2

You should use System.in and System.out instead of StdIn and StdOut.
Create an ObjectInputStream wrapping the System input stream the following way:
ObjectInputStream ois = new ObjectInputStream(System.in);
It has a readInt method, amd to check if it is empty, you must catch the EOFException.

Amir Kost
  • 2,148
  • 1
  • 16
  • 30